EE: Find and Replace plugin

My first plugin for EE: Find & Replace.

→ Download the Find & Replace plugin version 1.3.

This plugin works pretty much the same as the php str_replace() or preg_replace() function.

Expression Engine strips the white space from the beginning and the end of each parameter. Because of this, if you want to replace something with a space, use the string SPACE instead. You can also use the string QUOTE for a double quote: "

New in version 1.3 - 2006.11.24

  • Fixed a bug concerning replacing ampersands
  • Added regular expression find and replace: regex="yes"

Examples

Replace A with B:

{exp:replace find="you" replace="we"}
  text you want processed
{/exp:replace}

Result: text we want processed

Replace A with a space:

{exp:replace find="o" replace="SPACE"}
  text you want processed
{/exp:replace}

Result: text y u want pr cessed

Replace a space with nothing:

{exp:replace find="SPACE"}
  text you want processed
{/exp:replace}

Result: textyouwantprocessed

Replace A, B and C with D:

{exp:replace find="a|e|i|o|u" replace="X" multiple="yes"}
  text you want processed
{/exp:replace}

Result: tXxt yXX wXnt prXcXssXd

Replace A, B and C with X, Y and Z:

{exp:replace find="text|you|want" replace="words|we|have" multiple="yes"}
  text you want processed
{/exp:replace}

Result: words we have processed

Regular Expression find and replace:

{exp:replace find="\w+" replace="*" regex="yes"}
  text you want processed
{/exp:replace}

Result: * * * *

Regular Expression find and replace with backreference:

{exp:replace find="<a[^>]*href=QUOTE(.+)QUOTE[^>]*>(.*)</a>" replace="$2 ($1)" regex="yes"}
  <a href="http://www.foo.com/">text</a> you want <a href="http://www.bar.com/">processed</a>
{/exp:replace}

Result: text (http://www.foo.com/) you want processed (http://www.bar.com/)

Also…

If you want to replace something with nothing, best is to omit the replace parameter altogether. If you want to find multiple strings, always use the multiple="yes" parameter, or else it will search for the literal string, including vertical bars. The multiple parameter has no effect when using a regular expression find and replace.

This function is case sensitive by default. Use the parameter casesensitive="no" to ignore case, both for a normal as for a regular expression find and replace.

When using regex="yes" it is recommended that you set your Debug Preference (Admin > System Preferences > Output and Debugging Preferences) to 1, so Super Admins can make sure their regular expressions aren’t generating server errors.

February 1st 2005 | Add-Ons, ExpressionEngine | 66 comments


Comments

  1. 1 Joost February 1st 2005, 15:07

    Praat jij nou maar Nederlands.

  2. 2 Low February 1st 2005, 15:13

    @Joost: ‘t Is voor een internationaal publiek bedoeld, vandaar.

  3. 3 Joost February 1st 2005, 15:21

    Achzo :-)

  4. 4 Erwin Heiser October 11th 2006, 12:41

    Nice plugin, should come in handy somewhere.
    @joost: pipe down, laddy!

  5. 5 Alexo December 10th 2006, 03:56

    NIce plugin, just one question

    how to resplace [’ ] ?

    for example i have “Parkinson’s Disease” no to resplace with “Heart Disease”

    how can i do it ?

  6. 6 Low December 10th 2006, 11:33

    Alexo, you should probably make sure whether the single quote is converted to a html entity. Check the source code for this. You should probably use something like:

    {exp:replace find="Parkinson&#8217;s" replace="Heart"}

  7. 7 Alexo December 13th 2006, 20:18

    Hello there

    Thank you Low for this piece of code : ’ :-)

  8. 8 Magnus Wester February 12th 2007, 15:55

    Hi Low,
    I use your plug-in on my site. It removes the tags and control characters in the Summary field so I can use it in the Descriptions meta tag.
    Thanks a lot for the regular expressions support.
    Magnus

  9. 9 John Heasly February 14th 2007, 22:54

    I wasn’t able to get it to replace hard carriage returns with a space.

    Using:
    {exp:replace find=”(\w)+
    (\w+)” replace=”$1SPACE$2″ regex=”yes” multiple=”yes”}
    {/exp:replace}

    It found the hard returns but didn’t insert the space in the replace.

    i.e., with the text
    “Get rid of the hard return
    in this entry. And every other
    hard return too.”

    Result:
    “Get rid of the hard returnin this entry. And every otherhard return too.”

    And also the “find” would inexplicably skip some of the hard returns. But I’d be happy with getting the “replace” syntax right.

  10. 10 Magnus Wester February 15th 2007, 09:05

    John,
    i guess you could try working with the control characters directly instead, i.e.
    find=”/(\r\n|\r|\n)/” replace=”/SPACE/”
    (The above would support both the Windows, Macintosh and Unix end-of-line sequences; I don’t know if you need that but why not make it bullet-proof.)
    Multiple=”yes” means nothing with a regex so you can leave it out. The plug-in always replaces all strings that match your regex.
    Magnus

  11. 11 J Heasly February 15th 2007, 21:38

    Hey Magnus,

    Thanks for your post.

    Your suggested control characters method seems to work in the same hit-and-miss way as mine. The “\w+” pattern seems to skip words beginning with capital letters. Is this normal? And the hard carriage returns that are getting removed aren’t getting replaced with a space. And you were also right about not needing Multiple=”yes” in a regex.

    Here’s the pattern I used:
    {exp:replace find=”/(\w+)\r(\w+)/” replace=”/$1SPACE$2/” regex=”yes”}

    Just curious: Why do you have a slash character after each open quote and before each close quote?

    John

  12. 12 Low February 16th 2007, 00:01

    John, I got this working with the following pattern: {exp:replace find="(\r\n|\r|\n)" replace="SPACE" regex="yes"}

    But, are you sure that this is what you want? Maybe there’s formatting being applied to your text, so that carriage returns are being converted to <br />’s, which are left alone by the plugin. Do you have a working example online you can link to?

  13. 13 John Heasly February 16th 2007, 03:55

    Hello Low,

    Your solution works about the same as mine or Magnus’. That is, it catches some of the line breaks/hard returns and misses others and fails to replace any that it finds with a space.

    What I’m doing is using the Moblog module to turn e-mails into entries. The e-mailed entries come with a few bad high-ASCII characters and many line breaks/hard returns that I’m trying to remove by running them through some regiex-capable filter such as yours. The first thing I’d like to do is get rid of the line breaks, then maybe worry about some of the odd characters.

    In my template, the field that I’m running through your filter has the Xhtml formatting option, but it doesn’t seem to make a difference if it’s on or off.

    Thanks in advance for any help you can offer!

  14. 14 Low February 16th 2007, 10:13

    John, can you try this one?

    {exp:replace find="\s+" replace="SPACE" regex="yes"}

    That should replace any whitespace with one space. Also, if you can copy and paste your target text, you can try several regular expressions in this useful workbench.

  15. 15 Magnus Wester February 16th 2007, 10:31

    John,
    The slashes I use are just the normal regex delimiters, the same way you surround strings with quotes. You don’t need to use them since Low’s plugin neatly adds them for you if they’re missing. Sorry for any confusion.
    You have a complex data-related problem that may be beyond what can be expected of a simple find/replace utility. It would be fairly easy to build a “cleanser” plug-in for EE to do what you want.
    Otherwise, my next suggestion is that you simply reverse the pattern - have find/replace look for anything that isn’t in the allowed character set, and make that a space:

    find=”/[^\w]/” replace=”SPACE”

    Just add all remaining characters you want to allow inside the square brackets. As usual, some special characters need to be prefixed by a backslash.

  16. 16 David March 2nd 2007, 12:22

    Hey Low, Trying to use your find and replace plugin to convert lastname, firstname into firstname lastname.

    The first code I used was

    find=”(\w+), (\w+)” replace=”\2 \1″ regex=”yes”

    Which appears to work with singles names, ie “Joiners, The” gets replaced with “The Joiners”

    However “Educated Animals, The” gets replaced with “Educated The Animals” (it should be “The Educated Animals”)

    I was under the impression that the + would find one or more. so I thought it would find one or more words before the ,

    I then tried (.+), to see if matching any character many times would work. It didn’t it gave the same result as above.

    Using the reWork site you linked above the (.+), did link the first two words into a group the (\w+), didn’t.

    Any ideas what I’m doing wrong?

  17. 17 David March 2nd 2007, 12:26

    Scrap that. The (.+) does work. I was changing the wrong instance of it to test. Doh!

    Thanks for the excellent plugin.

  18. 18 David March 2nd 2007, 12:42

    Aha. You thought I’d let you off then didn’t you. Well unfortunately not.

    Using (.+), (\w+) for some reason converts “¡Forward Russia!” into “Russia ¡Forward!”

    (If ¡ doesn’t convert its a Inverted Exclamation Mark)

    The reWork site and other regex utilities leave it alone. Am I doing something wrong or have I found a bug?

  19. 19 Low March 2nd 2007, 12:53

    Hi David. Try this on for size:

    find="([^,]+), (\w+)" replace="$2 $1"

    That will look for anything but the comma in the first part. I think that should work.

  20. 20 David March 2nd 2007, 12:59

    Nope. Gives the same result. There’s no comma in this string so I’m confused as to why its matching it ¡Forward Russia! is correct and should be ignored by find and replace

  21. 21 Low March 2nd 2007, 13:18

    Strange. It works here. Can you point me to your example? Send me an email if it’s private.

  22. 22 David March 2nd 2007, 13:24

    Strange very strange. My Example You’ll have to login first. Its still closed Dev and if you want to look at the template its v4/numbers_inc

    Thanks in advance. I’ve probably done something stupid but I can’t see it

  23. 23 Low March 2nd 2007, 14:13

    David, I don’t seem to be able to access the v4 template group in your control panel. But I tested this code, which seemed to work:

    {exp:replace find="([^,]+), (.+)" replace="$2 $1" regex="yes"}{title}{/exp:replace}

    Note that I don’t use \w for the last part either. That’ll sort “Harvey, P.J.” out.

  24. 24 David March 2nd 2007, 14:25

    Ok. Very strange. The new regex fixes PJ Harvey (I hadn’t gotten around to spotting her yet) but forward Russia still doesn’t behave itself. I now get Russia! ¡Forward
    Huh, what’s going on?

    I created the v4 group after I created your member group. I’ve know fixed it so you can access it.

  25. 25 Low March 2nd 2007, 15:26

    I spotted it! You’ve actually got two Forward Russia entries in your bands section: “&#161;Forward, Russia!” and “Forward Russia!”. There’s your problem, right there. :)

  26. 26 David March 2nd 2007, 16:24

    hmmm. Well I deleted Forward Russia! and still get Russia! ¡Forward. Can I assume the special character is breaking something?

  27. 27 David March 2nd 2007, 16:56

    OK I don’t know what I did but its now working. I was just testing it out on a different bit of the page and it now works.

    Thanks for looking into this buddy.

  28. 28 Low March 2nd 2007, 17:00

    It works now, ’cause I removed the comma from the title, which was “&#161;Forward, Russia!” (sic). Note the comma in the middle. There’s the culprit. :)

  29. 29 David March 2nd 2007, 17:03

    *bleep* I never noticed that. You my friend are a genius.

  30. 30 Xacret.T May 10th 2007, 23:31

    Help me please.
    I have an image tag:
    /images/uploads/image/expressionengine.png
    and I need to get this:
    http://linka.ws/phpThumb/phpThumb.php?src=/images/uploads/image/expressionengine.png&w=300

    I’ve spend several hours trying understand regular expressions but it doesn’t helped… I’m not able to write this expression

  31. 31 David May 11th 2007, 09:39

    Hey Xacret. You don’t really need to use regex to do this.

    Say the only bit of the url that changes is the filename all you need to do is

    FIND /images/
    REPLACE phpThumb/phpThumb.php?src/images/

    That will leave the rest of your link unchanged but give you the phpthumb bit too.

  32. 32 Xacret.T May 11th 2007, 10:36

    Yes, I did it with simple search and replace.
    The link in my first comment was parsed.
    I too have to add a parameter to the end of src: &w=300
    I have an image tag:
    /images/uploads/image/expressionengine.png
    and I need to get this:
    phpThumb.src=”/images/uploads/image/expressionengine.png&w=300″

  33. 33 billg June 17th 2007, 16:27

    Hi,
    Does it work with custom fields? I’m trying to use it to replace a SPACE with a “+” in a Technorati tag that I’ve implemented as a custom field.

    Like this:

    {exp:replace find=”SPACE” replace=”+”}{Technorati-1}{/exp:replace}

    I.e., I enter something like “Word Word” as the tag, and need to replace the space with a plus sign so the link itself actually contains “word+word”.

    Thanks.

  34. 34 billg June 17th 2007, 18:47

    Oops. It was my mistake. Works just fine. Apologies.

  35. 35 AdamG July 4th 2007, 08:08

    Is there a way to capitalise using this plugin?

    I have this:

    {exp:replace find=”(\w+)-(\w+)” replace=”\1SPACE/SPACE\2″ regex=”yes”}{segment_2}{/exp:replace}

    Which turns this:
    “climate-change”
    Into this:
    “climate change”

    I would also like to capitalise the first letter of every word, so I can get:
    “Climate Change”

    Is this possible? Thanks very much!

  36. 36 Low July 4th 2007, 09:48

    Adam, you can use CSS to do just that: text-transform:capitalize;

  37. 37 AdamG July 5th 2007, 03:49

    Thanks Low!
    Unfortunately I need to use this in the <title>, so CSS won’t work.

  38. 38 Erwin Heiser August 23rd 2007, 17:45

    Hi, would it be possible to put in multiple regular expressions and then say multiple=”yes”?
    Something like this:
    {exp:replace find=”regex1|regex2|regex3″ replace=”X” multiple=”yes”}
    text you want processed
    {/exp:replace}
    I’m using the plugin to filter out some of the crap TinyMCE injects into its HTML…

  39. 39 Stanislav Majerski September 15th 2007, 19:29

    Thank you for this plugin. Just used it a project. Very handy and it made me finally to start learning RegEx.

  40. 40 Riob Q September 16th 2007, 23:30

    I’d like to use this on an email addresses:

    first.last@website.com

    so that it shows it like this: first last

    Essentially, replaces the dot with a space (I know this is possible) and then takes the @ and everything after that and replaces it with nothing. I know the first is easy. Second possible?

  41. 41 Riob Q September 16th 2007, 23:32

    Just fyi - the above post left out my email example. I basically want to take any email address and take out everything at the @ symbol and past it.

    Also, if there are dots in the first part to replace with a space which I know is possible.

  42. 42 Victor September 17th 2007, 05:10

    Yeah good day: i was just trying out some things and noticed that when you use:
    {exp:replace find="[br]" replace="<BR>"}
    something [br] more thing
    {/exp:replace}

    This does not work, any ideas is the [ ] is causing the problem ?

  43. 43 Low September 19th 2007, 09:37

    Victor, the [] shouldn’t be a problem. Are you using any other plugins in combination with this one?

  44. 44 Quena October 26th 2007, 15:09

    Thanks, all, for a lively and helpful discussion?

    To anybody who (like Magnus) is using this plugin to convert a custom field to the META description tag: Care to share your code, including the list of “find”s? I’m wondering how to catch all entities, special characters and HTML, without continually updating my templates as users discover new problems to throw at me.

    If you prefer to post your answer in the EE forums, clicking on my name will take you into the EE forum thread where I posted this question. If you post here, please let me know if you DON’T want me to cross-post it there.

    Thanks in advance!

  45. 45 Magnus Wester October 26th 2007, 16:23

    Hi Quena,
    Whatever I can contribute to solve your problem I added to your thread in the EE forums.
    Good luck!

  46. 46 Mark Bowen April 29th 2008, 15:06

    Hiya,

    Just wanted to say thanks for this plugin. I actually downloaded it yonks ago but came to using it today on something and realised that I hadn’t said thank you so here goes…

    … Thank you!!

    Keep up all the great work, it is very much appreciated.

    Best wishes,

    Mark

  47. 47 Tomelloso June 26th 2008, 20:52

    Hi, nice plugin. I trued this and work fine for me. Thanks and keep up good work mate.

  48. 48 Luke Stevens September 18th 2008, 07:42

    Hey Low, the replace plugin is a given on any EE installation I work on, so thanks for releasing it :)

    Just one random question - is there any way to replace a pipe | in a chunk of text? It’s not hugely important but I’m working with aggregated blog headlines that are something like:
    Some blog title | Blog Posts | My Cool Blog
    … which are fine for titles, but they look pretty ugly in my design, so I wouldn’t mind tweaking them a bit ;) If you have any suggestions it would be much appreciated, thanks!

  49. 49 Low September 21st 2008, 17:56

    @Luke Stevens: if you’re not using multiple="yes" or regex="yes", then replacing a pipe should work just as any other character.

  50. 50 Luke Stevens September 22nd 2008, 15:31

    Oh great, thanks for letting me know :)

  51. 51 MS November 10th 2008, 04:36

    Hi Low,
    I have been trying every possible combination of things to make this work. I am using your plugin in ee with a blog posting audio files from hipcast. Hipcast sends this:

    <iframe src=”http://www.hipcast.com/playweb?audioid=P85964305c5f19f6fd2df7dffdf1b1c73bFB+RFREYmN2&buffer=5&fc=FFFFFF&pc=CCFF33&kc=FFCC33&bc=FFFFFF&brand=1&player=ap21″ height=”20″ width=”246″ frameborder=”0″ scrolling=”no”> </iframe>

    Essentially i need to convert out the opening and closing carots and my most recent attempt i tried the following

    {exp:replace find=“<iframe;|scrolling=’no’>|</iframe>â€? replace=“<iframe|scrolling=’o'>|</iframe>â€? multiple=”yes”}

    I also tried just replacing using <| >

    Can you point me in the right direction?

  52. 52 Low November 10th 2008, 15:42

    @MS: I’m sorry, but I don’t understand what it is you’re trying to accomplish. Do you need to replace the opening and closing html-brackets with html-entities? In that case, you’d probably need something like find="<|>" replace="&lt;|&gt;" multiple="yes".

  53. 53 MS November 10th 2008, 19:02

    I am so sorry i didnt check that post carefully after submit

    this is what hipcast sends

    <br / >
    & l t ;iframe src=”http://www.hipcast.com/playweb?audioid=P85964305c5f19f6fd2df7dffdf1b1c73bFB+RFREYmN2&buffer=5&fc=FFFFFF&pc=CCFF33&kc=FFCC33&bc=FFFFFF&brand=1&player=ap21″ height=”20″ width=”246″ frameborder=”0″ scrolling=”no” & g t; & l t ;/ iframe & g t ;

    i added spaces so it wouldn’t convert.

    This is the code (one of many attempts) I used (again spaces added)

    {exp:replace find=“& l t ;| & g t ;â€? replace=“<|>â€? multiple=”yes”}

  54. 54 Ian November 20th 2008, 18:33

    Low said above: “@Luke Stevens: if you’re not using multiple=”yes” or regex=”yes”, then replacing a pipe should work just as any other character.”

    What if you need to replace a pipe character and use multiple=”yes”?

  55. 55 Low November 20th 2008, 19:01

    @Ian: with the current version (1.3), you can’t. I’ll need to update plugin to allow for such a functionality.

  56. 56 rh2600 November 23rd 2008, 10:23

    Works great :) but it doesn’t render out when inside the magpie plugin…

    {exp:magpie url=”http://www.google.com?search={exp:replace find=”SPACE” replace=”+;”}{title}{/exp:replace}” limit=”1″ refresh=”720″}

    any ideas?

  57. 57 Low November 23rd 2008, 12:11

    @rh2600: you can’t use a plugin as input for another plugin like that. You’ll have to find a way to do the replacing before you use it as a parameter. By using php on input, for example.

  58. 58 Beebs December 9th 2008, 00:02

    Thanks a lot for great plugin. A trully time-saver!

  59. 59 Nelly March 12th 2009, 03:45

    First of all, thanks for this great plugin. It helped me with a big problem I had with paths for a site’s mobile version.
    I think it’s necessary to escape any “/” in the regular expressions, and I didn’t see that in the documentation.

  60. 60 Albert March 12th 2009, 16:02

    Hi,
    thank you for this cool plugin. One question though: How do I replace a ” with nothing? I tried to escape it as \” but it didn´t work.
    I checked the thread in the EE forum, but couldn´t find anything.

    Thanks
    awa

  61. 61 Low March 12th 2009, 16:05

    Hey awa. Try this: {exp:replace find="QUOTE"} ... {/exp:replace}

  62. 62 Albert March 12th 2009, 16:36

    Dank u wel! That was a quick reply all is working now
    awa

  63. 63 Daemon March 13th 2009, 15:57

    Hi! Picture this. Simply text like “drums guitar piano” needs to be replaced with drums guitar piano.. So the word has to replaced with a hyperlink that forms a filename based on it’s orgin name.

    {exp:replace find=”\w+” replace=”<a href=”[(the value of \w+]+.mp3″> [(the value of \w+]</a>” regex=”yes”}
    drums guitar piano
    {/exp:replace}
    How far I am off? Kind regards, D’R

  64. 64 Low March 16th 2009, 14:11

    Hi Daemon. That’ll be something like find="(\w+)" replace="<a href='$1.mp3'>$1</a>" regex="yes", so you’re not far off.

  65. 65 Vishnu March 17th 2009, 20:06

    I have to replace as below and it works only few occurances, So correct me if this is wrong of the doing it.
    exp:replace find=”’|–|â€Â?|“|Â|â€â€?|…” replace=”‘|-|QUOTE|QUOTE|SPACE|SPACE-” multiple =”yes”}{body}{/exp:replace}

    Thanks,
    VishnuP

  66. 66 Low March 17th 2009, 20:13

    VishnuP, check the html source — you might need to look for html entities instead of the special characters.

Leave a comment



Some html (a, em, strong, etc) allowed.
Email won’t be displayed and takes care of your Gravatar.