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.
- 1 februari 2005, 14:10
- Add-Ons, ExpressionEngine
- 47 comments

Praat jij nou maar Nederlands.
@Joost: ‘t Is voor een internationaal publiek bedoeld, vandaar.
Achzo :-)
Nice plugin, should come in handy somewhere.
@joost: pipe down, laddy!
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 ?
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’s" replace="Heart"}Hello there
Thank you Low for this piece of code : ’ :-)
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
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.
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
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
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?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!
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.
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.
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?
Scrap that. The (.+) does work. I was changing the wrong instance of it to test. Doh!
Thanks for the excellent plugin.
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?
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.
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
Strange. It works here. Can you point me to your example? Send me an email if it’s private.
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
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.
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.
I spotted it! You’ve actually got two Forward Russia entries in your bands section: “¡Forward, Russia!” and “Forward Russia!”. There’s your problem, right there. :)
hmmm. Well I deleted Forward Russia! and still get Russia! ¡Forward. Can I assume the special character is breaking something?
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.
It works now, ’cause I removed the comma from the title, which was “¡Forward, Russia!” (sic). Note the comma in the middle. There’s the culprit. :)
*bleep* I never noticed that. You my friend are a genius.
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
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.
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″
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.
Oops. It was my mistake. Works just fine. Apologies.
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!
Adam, you can use CSS to do just that:
text-transform:capitalize;Thanks Low!
Unfortunately I need to use this in the <title>, so CSS won’t work.
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…
Thank you for this plugin. Just used it a project. Very handy and it made me finally to start learning RegEx.
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?
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.
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 ?
Victor, the [] shouldn’t be a problem. Are you using any other plugins in combination with this one?
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!
Hi Quena,
Whatever I can contribute to solve your problem I added to your thread in the EE forums.
Good luck!
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
Hi, nice plugin. I trued this and work fine for me. Thanks and keep up good work mate.