The attached add-on files may be out of date. Please visit Devot-ee for the latest versions.
Hi,
I have tweaked Lodewijk Schutte’s Find and Replace plugin. The result is a new plugin called ” Find and Replace Plus”.
This plugin works pretty much the same as the php str_replace() function: http://www.php.net/manual/en/function.str-replace.php and the preg_replace() function: http://www.php.net/manual/en/function.preg-replace.php
ExpressionEngine 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. Also in a parameter values use aliases for some other characters: “:QUOTE:” for double quote, “:SLASH:” for slash, “:LD:” for left curly brace and “:RD:” right curly brace.
PARAMETERS
1) find - Required. Allows you to specify what strings should be found. You can specify several strings using pipeline character.
2) replace - Optional. Allows you to specify what strings should replace the strings found. You can specify several strings using pipeline character. In case you leave this parameter undefined, the strings found will be replaced with empty string.
3) multiple - Optional. Allows you to specify if you seach for several strings (value “yes”), or for just one string (value “no”). Default is “no”.
4) casesensitive - Optional. Allows you to specify if you want to do casesensitive replace (value “yes”) or caseinsensitive replace (value “no”). Default is “yes”.
5) regex - Optional. Allows you to specify regular expression.
VARIABLE PAIRS
{replace_area}{/replace_area} - Optional. Allows you to specify area in which the plugin should do find-replace operations.
Each parameter can be used not only inside exp:replace_plus tag but also inside {replace_area} variable pair. In case parameter is defined both inside exp:replace_plus tag and inside {replace_area} variable pair, the value of the latter overrides the value of the former.
{replace_area}{/replace_area} can be nested. This feature can be handy in cases initial find-replace produces unwanted results. Those unwanted results can be corrected by wrapping one variable pair inside another pair having different “find” and “replace” parameters.
EXAMPLES
{exp:replace_plus find="you" replace="we"}
text you want processed
{/exp:replace_plus}
Result: text we want processed
{exp:replace_plus find="you" replace="we"}
text you want processed
{replace_area}
text you want processed
{/replace_area}
text you want processed
{/exp:replace_plus}
Result: text you want processed text we want processed text you want processed
{exp:replace_plus}
text you want processed
{replace_area find="you" replace="we"}
text you want processed
{/replace_area}
{replace_area find="you" replace="I"}
text you want processed
{/replace_area}
text you want processed
{/exp:replace_plus}
Result: text you want processed text we want processed text I want processed text you want processed
correct unwanted results by wrapping it within another {replace_area}{/replace_area} variable pair:
{exp:replace_plus}
text you want processed
{replace_area find=":SPACE:>:SPACE:" replace=">"}
{replace_area find="<|>" replace="<|>"}
{if total_results > 0}
text you want processed
{/if}
{/replace_area}
{/replace_area}
text you want processed
{/exp:replace_plus}
Result: text you want processed <p>text you want processed</p> text you want processed
{exp:replace_plus find="o" replace=":SPACE:"}
text you want processed
{/exp:replace_plus}
Result: text y u want pr cessed
{exp:replace_plus find=":SPACE:"}
text you want processed
{/exp:replace_plus}
Result: textyouwantprocessed
{exp:replace_plus find="a|e|i|o|u" replace="X" multiple="yes"}
text you want processed
{/exp:replace_plus}
Result: tXxt yXX wXnt prXcXssXd
{exp:replace_plus find="text|you|want" replace="words|we|have" multiple="yes"}
text you want processed
{/exp:replace_plus}
Result: words we have processed
{exp:replace_plus find="\\w+" replace="*" regex="yes"}
text you want processed
{/exp:replace_plus}
Result: * * * *
{exp:replace_plus 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_plus}
Result: text (http://www.foo.com/) you want processed (http://www.bar.com/)
Note: 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.
Changelog:
-1.3 // Variable pair {replace_area_start}{replace_area_end} changed into {replace_area}{/replace_area}. Added support for parameters inside {replace_area}{/replace_area} variable pair. Added support for nesting of {replace_area}{/replace_area}variable pairs. -1.2 // Added more aliases for characters which are illegal or problematic to use inside parameters’ values. -1.1 // Added support for multiple {replace_area_start}{replace_area_end} variable pairs. -1.0 // Initial release. Continued in the next post
Adding {replace_area}{/replace_area} variable pair might seem weird. Why one should think that wrapping some text to be processed with {replace_area}{/replace_area} is somehow better than simply wrapping it with {exp:replace_plus}{/exp:replace_plus} tag pair?
Well… the answer is that wrapping text to be processed with {replace_area}{/replace_area} variable pair instead of wrapping it simply with tag pair is sometimes really needed because it gives more control over parsing order.
Consider situation in which Find and Replace Plus plugin is used together with Reeposition plugin.
Say, we have such code:
{exp:reeposition}
{reeposition:put_item id="first_item"}
{reeposition:put_item id="second_item"}
{reeposition:item id="first_item"}
<ul> First list:
{exp:weblog:entries weblog="commentary" limit="5" sort="asc" sort_by="title"}
<li>{title}</li>
{/exp:weblog:entries}
</ul>
{/reeposition:item}
{reeposition:item id="second_item"}
<ul> Second List:
{exp:weblog:entries weblog="dictionary" limit="5" sort="asc" sort_by="title"}
<li>{title}</li>
{/exp:weblog:entries}
</ul>
{/reeposition:item}
<textarea cols="60" rows="10">
{exp:replace_plus find="<|>" replace="& lt;|& gt;" multiple="yes"}
{reeposition:put_item id="first_item"}
{reeposition:put_item id="second_item"}
{/exp:replace_plus}
</textarea>
{/exp:reeposition}
This code will not work: Reeposition plugin will move items, but Find and Replace Plus plugin will repace nothing since {exp:replace_plus} tag will be parsed before {exp:reeposition} tag.
Trying to add parse=”inward” parameter to {exp:reeposition} tag will not help: {exp:weblog:entries} will remain unparsed.
In this situation we need parsing order as follows: 1) {exp:weblog:entries} tag 2) {exp:reeposition} tag 3) {exp:replace_plus} tag but we cannot have such order because we cannot wrap {exp:reeposition} tag with {exp:replace_plus} tag - if we did the Find and Replace Plus plugin replaced characters inside and outside of <textarea> element.
We can achieve needed parsing order using {replace_area}{/replace_area} variable pair. This variable pair allows us to delimit area in which we want replace operation to be done and by delimiting such area it allows us to wrap with {exp:replace_plus} tag some other tag or tags.
That is, we will achieve needed parsing order by writing this code:
{exp:replace_plus find="<|>" replace="& lt;|& gt;" multiple="yes"}
{exp:reeposition}
{reeposition:put_item id="first_item"}
{reeposition:put_item id="second_item"}
{reeposition:item id="first_item"}
<ul> First list:
{exp:weblog:entries weblog="commentary" limit="5" sort="asc" sort_by="title"}
<li>{title}</li>
{/exp:weblog:entries}
</ul>
{/reeposition:item}
{reeposition:item id="second_item"}
<ul> Second List:
{exp:weblog:entries weblog="dictionary" limit="5" sort="asc" sort_by="title"}
<li>{title}</li>
{/exp:weblog:entries}
</ul>
{/reeposition:item}
<textarea cols="60" rows="10">
{replace_area}
{reeposition:put_item id="first_item"}
{reeposition:put_item id="second_item"}
{/replace_area}
</textarea>
{/exp:reeposition}
{/exp:replace_plus}
I have released version 1.2 of Find and Replace Plus plugin.
In this release more aliases were added for characters which are illegal or problematic to use inside parameters’ values.
Following aliases are currently supported:
:SPACE: === ’ ’
:QUOTE: === ‘“’
:SLASH: === ’/’
:LD: === ’{’
:RD: === ’}’
Use these aliases if you need to specify these characters inside “find” or inside “replace” parameter of exp:replace_plus tag.
I have released version 1.3 of Find and Replace Plus plugin.
In this release:
1) Variable pair {replace_area_start}{replace_area_end} changed into more usual for ExpressionEngine {replace_area}{/replace_area} variable pair.
2) Support for defining parameters inside {replace_area}{/replace_area} variable pair was added. Now each parameter can be used not only inside exp:replace_plus tag but also inside {replace_area} variable pair. In case parameter is defined both inside exp:replace_plus tag and inside {replace_area} variable pair, the value of the latter overrides the value of the former.
3) Added support for nesting of {replace_area}{/replace_area} variable pairs. This feature can be handy in cases initial find-replace produces unwanted results. Those unwanted results can be corrected by wrapping one variable pair inside another pair having different “find” and “replace” parameters.
I’m trying to strip out the https://ellislab.com/asset/images/team-photo/ that Mark Huot’s file extension is leaving in the code if I replace a directly uploaded image with one from the native file browser. I’ve tried:
{exp:weblog:entries weblog="{my_weblog}" sort="desc" limit="1" disable="member_data|trackbacks|pagination" parse="inward" status="not closed"}<h3>{title}</h3>{exp:replace_plus find=":LD:filedir_1:RD:"}
{if entry_photo}{entry_photo}{replace_area}{file_url}{/replace_area}{/entry_photo}{/if}{/exp:replace_plus}
{body}
{extended}{/exp:weblog:entries}
but it doesn’t change anything with or without the parse=”inward”. Am I missing something?
Hi smartpill,
It seems that you incorrectly place {exp:replace_plus}{/exp:replace_plus} tag pair and that parse=“inward” parameter is not needed.
The code should be as follows:
{exp:replace_plus find=":LD:filedir_1:RD:"}
{exp:weblog:entries weblog="{my_weblog}" sort="desc" limit="1" disable="member_data|trackbacks|pagination" status="not closed"}<h3>{title}</h3>
{if entry_photo}{entry_photo}{replace_area}{file_url}{/replace_area}{/entry_photo}{/if}
{body}
{extended}{/exp:weblog:entries}
{/exp:replace_plus}
One more question. I’m trying to use a regex expression so I can remove any directory regardless of number (example: https://ellislab.com/asset/images/team-photo/, https://ellislab.com/asset/images/ent-partner-logo/, https://ellislab.com/asset/images/ent-partner-work/, etc.) Should this work?
{exp:replace_plus find=":LD:filedir_([1-9]+):RD:" regex="yes"}
I’ve tried a few variations, but nothing happens. When I remove the regex expression and hard code the number it works, but I’d like it to be flexible. I’m no regex pro so I may have just done this wrong.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.