Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2009-01-25 17:52:58

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 999
Website

pax_grep: Search regular expressions and replace with something else.

pax_grep allows you to search content for patterns using regular expressions, and replace them with something else. This plugin is just like its helpful parent rah_replace, but it uses PHP’s preg_replace function instead of str_replace. If you don’t need fancy search patterns using regular expressions, use rah_replace instead.

I modified rah_replace to solve a very specific problem — I needed to replace the first occurrence of a pattern with one thing, but all other occurrences with something else. It might solve a problem for you too.

Last edited by johnstephens (2018-06-14 19:24:39)

Offline

#2 2009-02-25 00:47:30

smdnetau
Member
From: Sydney
Registered: 2008-10-09
Posts: 52
Website

Re: pax_grep: Search regular expressions and replace with something else.

How would I remove something like this:

<a href=”

and replace it with nothing?

Offline

#3 2009-02-25 02:19:19

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 999
Website

Re: pax_grep: Search regular expressions and replace with something else.

This formula will strip out the fragment you specified:

<txp:pax_grep from='|<a href="|' to="">
<a href="http://designop.us/">Link</a>
</txp:pax_grep>

Output: http://designop.us/">Link</a> (the closing </a> tag will not be rendered, but will cause invalid markup in this case.

I’ve tried a couple different methods to strip out all the markup in this example, including the leftover ">, but keep triggering an error. Maybe someone who understands preg_replace() would be willing to offer further insight? I’ve posted the source on GitHub— please feel free to fork and improve this plugin if you have the wisdom and inclination!

Last edited by johnstephens (2009-02-25 02:19:34)

Offline

#4 2009-02-25 02:23:15

smdnetau
Member
From: Sydney
Registered: 2008-10-09
Posts: 52
Website

Re: pax_grep: Search regular expressions and replace with something else.

Thanks for your reply. Sadly, programming wisdom doesn’t run in my family unfortunately :)

Offline

#5 2009-02-25 02:53:40

jm
Plugin Author
From: Missoula, MT
Registered: 2005-11-27
Posts: 1,746
Website

Re: pax_grep: Search regular expressions and replace with something else.

johnstephens wrote:

I’ve tried a couple different methods to strip out all the markup in this example, including the leftover ">, but keep triggering an error.

If you want to remove all HTML tags, try this regex: <[^>]*>. It’s cool that you’ve got it on GitHub too :).

Offline

#6 2010-10-01 00:17:49

jan
Member
From: Utrecht, The Netherlands
Registered: 2006-08-31
Posts: 71
Website

Re: pax_grep: Search regular expressions and replace with something else.

Please note that if you use a comma in your regex, for example [x]{2,4}
that this will cause an error, since the plugin explodes your from string with the comma delimiter.
If you need to use a comma in your regex, modify the code so it explode your string with another delimiter.


Kensington TXP powered rock

Offline

#7 2010-10-01 13:30:20

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 999
Website

Re: pax_grep: Search regular expressions and replace with something else.

Good catch, Jan. Version 0.2 adds the delimiter attribute so you can set your own delimiter, like so:

<txp:pax_grep
	from="'asparagus, macaroni'|'beets'"
	to="cat food|cats"
	delimiter="|">I like asparagus, macaroni and beets.</txp:pax_grep>

delimiter defaults to a comma, so you can still use that without specifying one.

Edit: Added linebreaks in code example for readability.

Last edited by johnstephens (2010-10-01 13:43:43)

Offline

#8 2010-10-01 13:33:44

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 999
Website

Re: pax_grep: Search regular expressions and replace with something else.

Please let me know if you spot any problems with this, or any other problems!

Offline

#9 2012-04-09 22:26:36

frickinmuck
Member
Registered: 2008-05-01
Posts: 118

Re: pax_grep: Search regular expressions and replace with something else.

This plugin doesn’t seem to be available anymore – does anyone have a link?


The AI does not hate you, nor does it love you, but you are made out of atoms which it can use for something else.

Offline

#10 2012-04-09 22:49:04

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 999
Website

Re: pax_grep: Search regular expressions and replace with something else.

Sorry, Frickinmuck! I took my site down for renovations, and I am working hammer and tongs to get it back up shortly— plugin delivery will resume service at the link above.

I sent you the installer-encoded text in a private message. Please let me know if you have any questions!

Anyone else who needs a copy of the plugin, feel free to contact me and I’ll get it to you right away. The source is still posted on Github for anyone who wants to compile it the zem_tpl.php way.

Offline

#11 2012-04-10 21:09:59

frickinmuck
Member
Registered: 2008-05-01
Posts: 118

Re: pax_grep: Search regular expressions and replace with something else.

Thanks, much appreciated!


The AI does not hate you, nor does it love you, but you are made out of atoms which it can use for something else.

Offline

#12 2013-09-02 16:26:13

jens31
Plugin Author
From: munich / dtschermani
Registered: 2008-08-25
Posts: 183
Website

Re: pax_grep: Search regular expressions and replace with something else.

niiiice. thank you.

Offline

Board footer

Powered by FluxBB