We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Plugin: Find and Replace Plus

Development and Programming

Laisvunas's avatar
Laisvunas
879 posts
16 years ago
Laisvunas's avatar Laisvunas

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

Replace A with B:

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

Result: text we want processed

Replace A with B inside {replace_area} and {/replace_area} variable pair:

{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

Replace A with B inside several {replace_area}{/replace_area} variable pairs:

{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

Replace A with B inside {replace_area}{/replace_area} variable pair and

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

Replace A with a space:

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

Result: text y u want pr cessed

Replace a space with nothing

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

Result: textyouwantprocessed

Replace A, B and C with D:

{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

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

{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

Regular Expression find and replace:

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

Result: * * * *

Regular Expression find and replace with backreference:

{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

       
Laisvunas's avatar
Laisvunas
879 posts
16 years ago
Laisvunas's avatar Laisvunas

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}
       
Low's avatar
Low
407 posts
16 years ago
Low's avatar Low

Hi Laisvunas,

Although you did adhere to the CC license, I’d appreciate it if you’d let me know you’re using my code for your plugins before you release them. Just a simple matter of politeness, really.

Thanks.

       
Laisvunas's avatar
Laisvunas
879 posts
16 years ago
Laisvunas's avatar Laisvunas

Hi Lodewijk,

Excuse me for this.

I considered my tweak as having very special purpose, not as trying to achieve some general improvement of your plugin.

       
Laisvunas's avatar
Laisvunas
879 posts
16 years ago
Laisvunas's avatar Laisvunas

I have released version 1.1 of Find and Replace Plus plugin.

In this release support for multiple {replace_area_start}{replace_area_end} variable pairs was added.

       
Laisvunas's avatar
Laisvunas
879 posts
16 years ago
Laisvunas's avatar Laisvunas

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.

       
Laisvunas's avatar
Laisvunas
879 posts
16 years ago
Laisvunas's avatar Laisvunas

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.

       
Laisvunas's avatar
Laisvunas
879 posts
16 years ago
Laisvunas's avatar Laisvunas

I have released version 1.3.1 of Find and Replace Plus plugin.

In this release a couple minor bugs were fixed and the code was cleaned up.

       
smartpill's avatar
smartpill
456 posts
16 years ago
smartpill's avatar smartpill

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?

       
Laisvunas's avatar
Laisvunas
879 posts
16 years ago
Laisvunas's avatar Laisvunas

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}
       
smartpill's avatar
smartpill
456 posts
16 years ago
smartpill's avatar smartpill

Thanks for taking a look. I did try that before also, but tried it again just in case and it still isn’t working. I’m still getting:

http://nhpt.org/images/uploads/photos/https://ellislab.com/asset/images/team-photo/nhpt_test.jpg
       
Laisvunas's avatar
Laisvunas
879 posts
16 years ago
Laisvunas's avatar Laisvunas

Hi smartpill,

You spotted a bug.

I fixed it and now the code I suggested earlier should work correctly.

So grab version 1.3.2 and let me know if something goes wrong.

       
smartpill's avatar
smartpill
456 posts
16 years ago
smartpill's avatar smartpill
Hi smartpill, You spotted a bug. I fixed it and now the code I suggested earlier should work correctly. So grab version 1.3.2 and let me know if something goes wrong.

The 1.3.2 update is working. Thanks!

       
smartpill's avatar
smartpill
456 posts
16 years ago
smartpill's avatar smartpill

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.

       
Laisvunas's avatar
Laisvunas
879 posts
16 years ago
Laisvunas's avatar Laisvunas

Hi smartpill,

That’s the same bug in another place of plugin’s code. I fixed it and now your regex (which seems correct to me although I’m also to regex pro) should work.

So grab the version 1.3.3 and let me know if something goes wrong.

       
1 2 3

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.