Plugin: PHPStringFun
Posted: 05 September 2006 01:18 AM   [ Ignore ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  962
Joined  12-20-2002

In response to a particular problem posed here on the forums, I copy’n'pasted together a quick plugin that acts as a gateway to PHP’s string functions without needing to activate PHP in a template. It’s attached to this post.

Derek made me improve it a bit, but he doesn’t want to add it to the main plugins repository until I’ve weeded out from it any PHP functions that won’t work within it. Rather than sit and test every PHP function, most of which I’ve never used and don’t know, I thought it might be easier to ask the community if they can see from the following list of PHP functions which ones will be problematic inside PHPStringFun.

addcslashes, bin2hex, chop, chr, chunk_split, convert_cyr_string, convert_uudecode, convert_uuencode, count_chars, crc32, crypt, echo, explode, get_html_translation_table, hebrev, hebrevc, html_entity_decode, htmlentities, htmlspecialchars_decode, htmlspecialchars, join, levenshtein, localeconv, ltrim, md5_file, md5, metaphone, money_format, nl_langinfo, nl2br, number_format, ord, parse_str, print, printf, quoted_printable_decode, quotemeta, rtrim, setlocale, sha1_file, sha1, similar_text, soundex, sprintf, sscanf, str_ireplace, str_pad, str_repeat, str_replace, str_rot13, str_shuffle, str_split, str_word_count, strcasecmp, strchr, strcmp, strcoll, strcspn, strip_tags, stripcslashes, stripos, stripslashes, stristr, strlen, strnatcasecmp, strnatcmp, strncasecmp, strncmp, strpbrk, strpos, strrchr, strrev, strripos, strrpos, strspn, strstr, strtok, strtolower, strtoupper, strtr, substr_compare, substr_count, substr_replace, substr, trim, ucfirst, ucwords, vfprintf, vprintf, vsprintf, wordwrap.

Whichever ones anybody points out I’ll remove from the array of allowed functions inside the plugin.

Hey, it’s worth a try.

File Attachments
pi.phpstringfun.txt  (File Size: 4KB - Downloads: 420)
 Signature 

Who ain’t a slave? - Ishmael

Profile
 
 
Posted: 14 November 2006 04:11 PM   [ Ignore ]   [ # 1 ]  
Moderator
Avatar
RankRankRankRankRankRankRankRank
Total Posts:  32910
Joined  05-14-2004

Do you know I never saw this? I added it to the wiki.

 Signature 
Profile
MSG
 
 
Posted: 15 November 2006 05:28 AM   [ Ignore ]   [ # 2 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  879
Joined  02-05-2002

I remember seeing this post but I was to busy to try the plugin at that time… and forgot about it red face

Great plugin, just did some testing and I absolutely love it!

Thanks Adam.

[EDIT] @Lisa, thanks for adding it to the Wiki.

 Signature 

Member of the EE Pro Network

Profile
 
 
Posted: 17 November 2006 03:49 PM   [ Ignore ]   [ # 3 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1735
Joined  03-26-2006

In my effort to remove as much PHP from my templates as possible, I will give this a shot. I just found a place where I’m using ‘str_replace’ so I’ll try re-doing that with this plugin, thanks!

 Signature 

ryan masuga
—————
Masuga Design | Member, EE Pro Network
My EE Add-Ons | {devot:ee}
Twitter: masuga | masugadesign | devot_ee

Profile
 
 
Posted: 17 November 2006 04:23 PM   [ Ignore ]   [ # 4 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1735
Joined  03-26-2006

Is there any obvious reason this isn’t working?

{exp:phpstringfun function="str_replace" par1="/" par2="x"}/blah/blah2/blah3{/exp:phpstringfun}


Results in:

x


That should replace every slash with an x, but it looks like it’s replacing the first one and stopping there. Any suggestions?

 Signature 

ryan masuga
—————
Masuga Design | Member, EE Pro Network
My EE Add-Ons | {devot:ee}
Twitter: masuga | masugadesign | devot_ee

Profile
 
 
Posted: 17 November 2006 04:24 PM   [ Ignore ]   [ # 5 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  12416
Joined  04-29-2002

Ryan, there already is a str_replace plugin. smile I don’t have the link to it handy, but you should be able to find it in the plugins list.

 Signature 

Quick Reference - EE Trial Options - EE Wiki - Docs for updating a build

Profile
MSG
 
 
Posted: 17 November 2006 05:11 PM   [ Ignore ]   [ # 6 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1735
Joined  03-26-2006

Why, you’re right! Thanks!

For anyone else: Here’s the thread for the Find and Replace plugin, as created by Lodewijk. This plugin can also found on the Plugins page.

 Signature 

ryan masuga
—————
Masuga Design | Member, EE Pro Network
My EE Add-Ons | {devot:ee}
Twitter: masuga | masugadesign | devot_ee

Profile
 
 
Posted: 27 December 2007 06:02 PM   [ Ignore ]   [ # 7 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  231
Joined  07-09-2006

does anyone know what happened to the phpstringfun plugin? Can’t find it anymore!

 Signature 

Erwin van Lun
Marketeer on a Mission

Profile
 
 
Posted: 27 December 2007 08:34 PM   [ Ignore ]   [ # 8 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1735
Joined  03-26-2006

Wait…isn’t it at the top of this thread?

 Signature 

ryan masuga
—————
Masuga Design | Member, EE Pro Network
My EE Add-Ons | {devot:ee}
Twitter: masuga | masugadesign | devot_ee

Profile
 
 
Posted: 20 February 2008 02:03 PM   [ Ignore ]   [ # 9 ]  
Summer Student
Avatar
Total Posts:  2
Joined  11-06-2006

i know this was posted a while ago, but i didn’t know about it until today. THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU (ad infinitum)

Profile
 
 
Posted: 22 March 2008 01:07 PM   [ Ignore ]   [ # 10 ]  
Lab Assistant
RankRank
Total Posts:  141
Joined  03-05-2008

Don’t forget: when uploading to the plugins folder, change the extension from ...

  pi.phpstringfun.txt

... to ...

  pi.phpstringfun.php

Documentation is then available in the Plugins manager by clicking on the plugin name/link.

Profile
 
 
Posted: 27 April 2008 02:15 AM   [ Ignore ]   [ # 11 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  137
Joined  12-24-2007

Mega-super-awesome-cool plugin.

 Signature 

Trifecta Interactive Marketing | Austin, TX

Profile
 
 
Posted: 10 June 2008 08:09 AM   [ Ignore ]   [ # 12 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  884
Joined  02-15-2008

This plugin is frickin’ excellent. So outstanding in fact, that today will forever be known to me as ‘phpstring fun day’. Mark it down everyone, 10th June.

 Signature 

Andy Harris | Pepper Digital | Malvern, UK | Twitter | New to ExpressionEngine? Start here!
Remember - If at first you don’t succeed, you’re not Chuck Norris

Profile
 
 
Posted: 10 June 2008 08:45 AM   [ Ignore ]   [ # 13 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  962
Joined  12-20-2002

A toast!

 Signature 

Who ain’t a slave? - Ishmael

Profile
 
 
Posted: 14 June 2008 07:05 AM   [ Ignore ]   [ # 14 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  884
Joined  02-15-2008

Has anyone gotten this to work with money_format? I have a string and I want to display it formatted in good old British Pounds Sterling. But I can’t get it to work…

 Signature 

Andy Harris | Pepper Digital | Malvern, UK | Twitter | New to ExpressionEngine? Start here!
Remember - If at first you don’t succeed, you’re not Chuck Norris

Profile
 
 
Posted: 14 June 2008 07:35 AM   [ Ignore ]   [ # 15 ]  
Research Scientist
Avatar
RankRankRankRankRankRank
Total Posts:  6579
Joined  04-15-2006

Hi Andy,

Just gave it a try here and couldn’t get it to work but I am wondering looking at the documentation page on php.net for money_format and seeing these notes :

Note: The function money_format() is only defined if the system has strfmon capabilities. For example, Windows does not, so money_format() is undefined in Windows.

Note: The LC_MONETARY category of the locale settings, affects the behavior of this function. Use setlocale() to set to the appropriate default locale before using this function.

Wonder if those have anything to do with it at all? There is at least one other function in this plugin which I couldn’t get to work some time back. Can’t quite remember which one it was now but overall this really is a fantastic plugin. I see the way it works so I am thinking that this is probably more a server issue than the plugin. I could be wrong on that though.

Don’t know if that helps at all?

Best wishes,

Mark

 Signature 

Full List Of Plugins Here!! (16)
 
Retrieve Statuses
Maximum Posts Reached
Neat Link
Redirect
Fetch URI

Profile
 
 
Posted: 21 June 2008 09:31 PM   [ Ignore ]   [ # 16 ]  
Summer Student
Avatar
Total Posts:  17
Joined  09-14-2006

Will this work within a weblog tag? I’m able to get it to work on hard-coded text, but that seems kinda pointless, I need it to work on the fly for a weblog:entries tag, kinda like so:

{exp:weblog:entries weblog="default"}
     {exp
:phpstringfun function="ltrim" par1="status:"}{body}{/exp:phpstringfun}<br />
{/exp:weblog:entries}

So basically, if I was pulling in a list of items from a weblog, any items that had the text “status:”, would have that part trimmed.

The above code works if I replace {body} with hard-coded text (i.e. “Status: blah blah”) and would return just the “blah blah” portion. As soon as I go back to the {body} tag it doesn’t work.

Any ideas? Thanks!

Profile
 
 
Posted: 21 June 2008 09:51 PM   [ Ignore ]   [ # 17 ]  
Summer Student
Avatar
Total Posts:  17
Joined  09-14-2006

...and to make matters slightly more confusing…the above works when I use {title} tag, just not {body} tag. Like so:

{exp:weblog:entries weblog="default"}
     {exp
:phpstringfun function="ltrim" par1="status:"}{title}{/exp:phpstringfun}<br />
{/exp:weblog:entries}

Profile
 
 
Posted: 22 June 2008 01:27 PM   [ Ignore ]   [ # 18 ]  
Summer Student
Avatar
Total Posts:  17
Joined  09-14-2006

I figured it out! When I was trying to get {body} it had a <p> tag by default at the start, so doing a ltrim failed since the first item was the <p> tag. I created a custom field for this instance so that I could set it as an input field vs. a text area so that it wouldn’t be wrapped by a <p> tag….that did the trick.

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1149, on July 16, 2007 09:33 AM
Total Registered Members: 65023 Total Logged-in Users: 29
Total Topics: 82108 Total Anonymous Users: 14
Total Replies: 441265 Total Guests: 227
Total Posts: 523373    
Members ( View Memberlist )