rah_replace
Published under current TXP projects
A tiny Textpattern plugin that returns contained content with all searched occurrences replaced with given replacements. This ideally works in same way as PHP’s str_replace() function, but allows you to use a simple XHTML stylish Textpattern tag.
Image: rah_replace
Intro and description
The plugin, rah_replace, returns contained/wrapped content with all occurances replaces with given replacements. It supports multiple replacemens and comes with a simple tag.
Attributes
The tag is a container <txp:rah_replace>###</txp:rah_replace> and attributes follow.
from
String/thing or multiple strings that will be searched and replaced with to attribute’s values. Comma (or delimiter value) seperated if multiple.
Default: from="" Example: "dog,cat,house"
to
Replacement or multiple replacements that will be used to replace from attribute’s occurances. Comma (or delimiter value) seperated if multiple.
Default: to="" Example: "ship,home,hat"
delimiter
Set the seperator/delimiter that is used in from and to values to seperate multiple needles/replacements. Default is comma.
Default: delimiter="," Example: "|"
Examples
Example #1: Replace a single dog to a cat.
<txp:rah_replace from="dog" to="cat">
My favorite animal is a dog.
</txp:rah_replace>
Outputs: My favorite animal is a cat.
Example #2: Replace multiple words with multiple replacements.
<txp:rah_replace from="house,dog,Mike" to="boat,friend,wife">
I live in a house with my dog and Mike.
</txp:rah_replace>
Outputs: I live in a boat with my friend and wife.
Example #3: Replace multiple words with a one replacement.
<txp:rah_replace from="house,Mike,dad" to="ship">
Last year we build our new house with Mike, and my dad.
</txp:rah_replace>
Outputs: Last year we build our new ship with ship and my ship.
Example #4: Use a different delimiter.
<txp:rah_replace from="house|Mike,|dad" to="ship|dog.|ball" delimiter="|">
Last year we build our new house with Mike, and my dad.
</txp:rah_replace>
Ouputs: Last year we build our new ship with dog. and my ball.
Changelog
Version 0.2
- Added a new attribute:
delimiter.
Version 0.1
- First release