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.

Comments

  1. author
    1 Joost 1 februari 2005, 15:07

    Praat jij nou maar Nederlands.

  2. author
    2 Low 1 februari 2005, 15:13

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

  3. author
    3 Joost 1 februari 2005, 15:21

    Achzo :-)

  4. author
    4 Erwin Heiser 11 oktober 2006, 12:41

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

  5. author
    5 Alexo 10 december 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. author
    6 Low 10 december 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. author
    7 Alexo 13 december 2006, 20:18

    Hello there

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

  8. author
    8 Magnus Wester 12 februari 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. author
    9 John Heasly 14 februari 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. author
    10 Magnus Wester 15 februari 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. author
    11 J Heasly 15 februari 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. author
    12 Low 16 februari 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. author
    13 John Heasly 16 februari 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. author
    14 Low 16 februari 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. author
    15 Magnus Wester 16 februari 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. author
    16 David 2 maart 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. author
    17 David 2 maart 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. author
    18 David 2 maart 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. author
    19 Low 2 maart 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. author
    20 David 2 maart 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. author
    21 Low 2 maart 2007, 13:18

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

  22. author
    22 David 2 maart 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. author
    23 Low 2 maart 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. author
    24 David 2 maart 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. author
    25 Low 2 maart 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. author
    26 David 2 maart 2007, 16:24

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

  27. author
    27 David 2 maart 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. author
    28 Low 2 maart 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. author
    29 David 2 maart 2007, 17:03

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

  30. author
    30 Xacret.T 10 mei 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. author
    31 David 11 mei 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. author
    32 Xacret.T 11 mei 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. author
    33 billg 17 juni 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. author
    34 billg 17 juni 2007, 18:47

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

  35. author
    35 AdamG 4 juli 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. author
    36 Low 4 juli 2007, 09:48

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

  37. author
    37 AdamG 5 juli 2007, 03:49

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

  38. author
    38 Erwin Heiser 23 augustus 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. author
    39 Stanislav Majerski 15 september 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. author
    40 Riob Q 16 september 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. author
    41 Riob Q 16 september 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. author
    42 Victor 17 september 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. author
    43 Low 19 september 2007, 09:37

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

  44. author
    44 Quena 26 oktober 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. author
    45 Magnus Wester 26 oktober 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. author
    46 Mark Bowen 29 april 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. author
    47 Tomelloso 26 juni 2008, 20:52

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

Leave a comment

Some html (a, em, strong, etc) allowed.


Zoeken in loweblog.com