Plugin: Find and Replace Plus
Posted: 28 August 2008 01:56 AM   [ Ignore ]  
Lab Assistant
Avatar
RankRank
Total Posts:  284
Joined  02-02-2007

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}
<p>text you want processed</p>
{/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

File Attachments
pi.replace_plus_v1.3.1.zip  (File Size: 5KB - Downloads: 21)
 Signature 

Full list of plugins here

Child Categories
Browser Sniff
Category Id
Entries List

Profile
 
 
Posted: 28 August 2008 02:24 AM   [ Ignore ]   [ # 1 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  284
Joined  02-02-2007

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}

 Signature 

Full list of plugins here

Child Categories
Browser Sniff
Category Id
Entries List

Profile
 
 
Posted: 28 August 2008 02:31 AM   [ Ignore ]   [ # 2 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  371
Joined  10-18-2004

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.

 Signature 

Low: the Pro Network member who brought you the File Manager Module, Yearly Archives Plugin, Find & Replace Plugin, Akismet Combo, and more…

Profile
 
 
Posted: 28 August 2008 02:57 AM   [ Ignore ]   [ # 3 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  284
Joined  02-02-2007

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.

 Signature 

Full list of plugins here

Child Categories
Browser Sniff
Category Id
Entries List

Profile
 
 
Posted: 30 August 2008 03:36 PM   [ Ignore ]   [ # 4 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  284
Joined  02-02-2007

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.

 Signature 

Full list of plugins here

Child Categories
Browser Sniff
Category Id
Entries List

Profile
 
 
Posted: 06 October 2008 01:13 AM   [ Ignore ]   [ # 5 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  284
Joined  02-02-2007

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.

 Signature 

Full list of plugins here

Child Categories
Browser Sniff
Category Id
Entries List

Profile
 
 
Posted: 27 October 2008 02:48 AM   [ Ignore ]   [ # 6 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  284
Joined  02-02-2007

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.

 Signature 

Full list of plugins here

Child Categories
Browser Sniff
Category Id
Entries List

Profile
 
 
Posted: 02 November 2008 03:42 AM   [ Ignore ]   [ # 7 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  284
Joined  02-02-2007

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.

 Signature 

Full list of plugins here

Child Categories
Browser Sniff
Category Id
Entries List

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1149, on July 16, 2007 09:33 AM
Total Registered Members: 64912 Total Logged-in Users: 32
Total Topics: 81864 Total Anonymous Users: 15
Total Replies: 440109 Total Guests: 204
Total Posts: 521973    
Members ( View Memberlist )
Newest Members:  bjmohrAqua193Bios Elementmjpoteetguimogranwelshmrcfthenetmonkeybenekwhobutsb