Content last modified Monday 6 March 2017
hop to #bottom
Categories:
Author:
Current version:
0.1.1
About It:
Get it:
Rate it:
  • Helpful?
  • 3 Yes
  • 0 No
Flag it:

If you'd like to provide updated information and do not have access to directly edit, please contact the site admin; thanks!

pax_grep

pax_grep is a plugin for Textpattern that allows you to find occurrences of a regular expression pattern and replace them with something else. It’s almost identical to it’s awesome parent plugin rah_replace by Jukka Svahn. pax_grep uses PHP’s preg_replace() function, packaged in a compact and easy-to-use Textpattern tag.

  • Version: 0.1.1
  • Updated on: 2009-01-25

Installaing/Uninstalling

In Textpattern, navigate to the “Plugins” tab under “Admin”, and paste the code into the “Install plugin” pane. Install and enable the plugin.

To uninstall, simply delete the plugin from the “Plugins” tab.

Usage

pax_grep takes a regular expression and searches content for matching patterns. Then it replaces matches with the string(s) you supply. This works just like rah_replace. If you don’t need to search and replace based on a regular expression, you should use rah_replace instead.

Syntax

pax_grep is a container tag with two attributes.

<txp:pax_grep ↩
	from="/search pattern/" ↩
	to="replacement text">
	Content
</txp:pax_grep>

Attributes

from

Give this attribute the value of a pattern or patterns you wish to search for. A pattern should be delimited with single quotes or some other character not used in the pattern. Separate multiple patterns with commas.

Default: from=""
Example: from="'^foo','bar$'"

to

This attribute holds the replacement value(s) for each pattern in the from attribute. No delimiters are needed, but multiple values must be separated by commas. Each from value will be replaced with the corresponding to value.

Default: to=""
Example: to="fox,bat"

Examples

Replace all vowels with hyphens.

This example searches for the any vowel, replacing each one with a hyphen.

<txp:pax_grep from="'[aeiou]'" to="-">
	My favorite animal is a dog.
</txp:pax_grep>
Result: M- f-v-r-t- -n-m-l -s – d-g.

Get words out of a request_uri.

This example outputs the current request URI, and uses a regular expression to drop the leading slash and transform many delimiter characters into spaces.

<txp:pax_grep ↩
	from="'^\/','\/','%20','\-','\+','\?=','\_'" ↩
	to=", , , , , , ">
	<txp:page_url type="request_uri" />
</txp:pax_grep>

If the browser is pointed to example.com/section-name/my-article-title/, pax_grep will return the following:

section name my article title

Changelog

Version 0.1.1

  • 2009-01-25: Changed plugin name from opus_grep to pax_grep in compliance with defacto standard of thre-letter prefix for plugins.

Version 0.1

  • 2009-01-23: Branched from rah_replace by Jukka Svahn— changed str_replace() to preg_replace().
Article Request Count:
Keywords/tags:
Initially released:
2009-01-23
Posted here:
23 Jan 2009
Article modified:
19 Jun 2009

If there is a comment form at the Information URL, you may want to leave your comments/questions there or at the Forum thread for quicker feedback. Otherwise, comment away:

Your comment will NOT be submitted until you click the 'Submit' button on the next pageload.

Commented (6)

Jakob:

An idea: would it be possible to add some case change attributes, e.g. ucwords/ucfirst/lowercase/uppercase=“1” and so on along the lines of ob1_changecase for the output?

Reason: sometimes I use the url title as a short form of a long title for a menu item.

Nitpicking: grep only searches for patterns and doesn’t do replacement. sed would be a little closer to what your plugin does :).

Current address for this plugin seems to be this:

http://designop.us/made/pax-grep

> Current address for this plugin seems to be…

The same as it always was. Is there an alternate link floating around somewhere?

Oh, I just realised that indeed it is the same URL address, but in this article that Download File link is malformed.

Thanks, Petri! It should be fixed now.

Subscribe to this article's comments RSS feed. [ ? ]   View Recent Comments across the site.

Would you care to write a comment or visit the forum discussion?

You know you want to visit the Archives.

Browse

Categories


Articles

There are also tag clouds, 'cause those are fun.
Published with Textpattern