1 of 2
1
Plugin: Find and Replace Plus
Posted: 28 August 2008 02:56 AM   [ Ignore ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1135
Joined  02-02-2007

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}
<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</ayou 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

 Signature 

Full list of add-ons here

Child Categories
Browser Sniff
Category Id
Entries Number

Profile
 
 
Posted: 28 August 2008 03:24 AM   [ Ignore ]   [ # 1 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1135
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"}
<ulFirst 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"}
<ulSecond 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"}
<ulFirst 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"}
<ulSecond 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 add-ons here

Child Categories
Browser Sniff
Category Id
Entries Number

Profile
 
 
Posted: 28 August 2008 03:31 AM   [ Ignore ]   [ # 2 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  284
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 Low Variables, Low Reorder, Low NoSpam, Low Seg2Cat and many more...

Profile
 
 
Posted: 28 August 2008 03:57 AM   [ Ignore ]   [ # 3 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1135
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 add-ons here

Child Categories
Browser Sniff
Category Id
Entries Number

Profile
 
 
Posted: 30 August 2008 04:36 PM   [ Ignore ]   [ # 4 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1135
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 add-ons here

Child Categories
Browser Sniff
Category Id
Entries Number

Profile
 
 
Posted: 06 October 2008 02:13 AM   [ Ignore ]   [ # 5 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1135
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 add-ons here

Child Categories
Browser Sniff
Category Id
Entries Number

Profile
 
 
Posted: 27 October 2008 03:48 AM   [ Ignore ]   [ # 6 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1135
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 add-ons here

Child Categories
Browser Sniff
Category Id
Entries Number

Profile
 
 
Posted: 02 November 2008 03:42 AM   [ Ignore ]   [ # 7 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1135
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 add-ons here

Child Categories
Browser Sniff
Category Id
Entries Number

Profile
 
 
Posted: 27 December 2008 07:03 PM   [ Ignore ]   [ # 8 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  524
Joined  09-06-2006

I’m trying to strip out the http://ellislab.com/images/uploads/ 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}
<img class="entry_photo" src="{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?

 Signature 

Kyle Summer | smartpill design | New Haven, CT |  twitter

Profile
 
 
Posted: 28 December 2008 02:40 AM   [ Ignore ]   [ # 9 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1135
Joined  02-02-2007

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}<img class="entry_photo" src="{replace_area}{file_url}{/replace_area}" />{/entry_photo}{/if}
{body}
{extended}{
/exp:weblog:entries}
{
/exp:replace_plus} 
 Signature 

Full list of add-ons here

Child Categories
Browser Sniff
Category Id
Entries Number

Profile
 
 
Posted: 28 December 2008 03:11 AM   [ Ignore ]   [ # 10 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  524
Joined  09-06-2006

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:

<img class="entry_photo" src="http://nhpt.org/images/uploads/photos/{filedir_1}nhpt_test.jpg" /> 
 Signature 

Kyle Summer | smartpill design | New Haven, CT |  twitter

Profile
 
 
Posted: 28 December 2008 06:28 AM   [ Ignore ]   [ # 11 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1135
Joined  02-02-2007

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.

 Signature 

Full list of add-ons here

Child Categories
Browser Sniff
Category Id
Entries Number

Profile
 
 
Posted: 28 December 2008 01:01 PM   [ Ignore ]   [ # 12 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  524
Joined  09-06-2006
Laisvunas - 28 December 2008 11:28 AM

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!

 Signature 

Kyle Summer | smartpill design | New Haven, CT |  twitter

Profile
 
 
Posted: 28 December 2008 07:45 PM   [ Ignore ]   [ # 13 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  524
Joined  09-06-2006

One more question. I’m trying to use a regex expression so I can remove any directory regardless of number (example: http://ellislab.com/images/uploads/, http://expressionengine.com/files/templates/zips/, http://expressionengine.com/files/templates/screenshots/, 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.

 Signature 

Kyle Summer | smartpill design | New Haven, CT |  twitter

Profile
 
 
Posted: 29 December 2008 06:33 AM   [ Ignore ]   [ # 14 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1135
Joined  02-02-2007

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.

 Signature 

Full list of add-ons here

Child Categories
Browser Sniff
Category Id
Entries Number

Profile
 
 
Posted: 29 December 2008 07:39 PM   [ Ignore ]   [ # 15 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  524
Joined  09-06-2006

I didn’t break it!  wink Seems to be working now. Thanks again.

 Signature 

Kyle Summer | smartpill design | New Haven, CT |  twitter

Profile
 
 
Posted: 23 October 2009 05:02 PM   [ Ignore ]   [ # 16 ]  
Grad Student
Rank
Total Posts:  49
Joined  12-22-2005

I have a template in ExpressionEngine setup to act as a TXT export of weblog entry data. Since the dataset contains a lot of natural language punctuation (commas, apostrophes, spaces, line breaks, etc.), I am using pipes to act as the delimiter between custom fields.

What I’d like to do is remove all of the line breaks [that’s contained in the custom fields] and replace them with spaces, so the information from the rendered template has each result on a single line. For example:

Heading || Heading || Heading
Line 1 
|| Data || Data || Data
Line 2 
|| Data || Data || Data
Line 3 
|| Data || Data || Data
Line 4 
|| Data || Data || Data
Line 5 
|| Data || Data || Data 

Much of the data often contains intermixed soft and hard returns (and other special characters pasted from Microsoft Word documents no less!), so the actual export looks more like:

Heading || Heading || Heading
Line 1 
|| Data || Data || Data
Line 2 
|| Data || Data
|| Data
Line 3 
|| Data || Data


|| Data
Line 4 
|| Data || Data || Data
Line 5 
|| Data || Data || Data 

When using the Find & Replace Plus plugin, I can find one or more newlines with the following Regular Expression:

{exp:replace_plus find="\n+" replace=" " regex="yes"

What I’d like help with is determining the Regular Expression to remove all types of newlines and linebreaks (\r, \n, \r\n, soft returns, hard returns, etc) and allow a single line per result from the weblog entries loop.

Does anyone have experience using the Find & Replace Plus plugin to accomplish this?

Profile
 
 
Posted: 24 October 2009 06:10 AM   [ Ignore ]   [ # 17 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1135
Joined  02-02-2007

Hi Ryan,

Maybe Oliver Heine’s Strip Linebreaks plugin can help you?

 Signature 

Full list of add-ons here

Child Categories
Browser Sniff
Category Id
Entries Number

Profile
 
 
Posted: 01 December 2009 10:52 AM   [ Ignore ]   [ # 18 ]  
Grad Student
Avatar
Rank
Total Posts:  89
Joined  10-29-2009

Hi

Why can’t I put search term inside Find?

{exp:replace_plus find="{exp:search:keywords}" replace="<h1>{exp:search:keywords}</h1>"}
{exp
:replace_plus} 

If I put it manually, then its OK.

Thanks in advance smile

Profile
 
 
   
1 of 2
1