Textpattern CMS template tag that returns contained content with all searched occurrences replaced with given replacements, in the same manner as PHP’s str_replace function.
Using Composer:
$ composer require rah/rah_replaceOr download an installer package.
The plugin, rah_replace, introduces a new container tag to Textpattern’s arsenal. The tag returns wrapped content with all found occurrences replaced with given replacements. A tag supports multiple searched occurrences and replacements.
<rah::replace from="value1, value2, ..." to="value1, value2, ...">
Searched content
</rah::replace><rah::replace>
...contained statement...
</rah::replace>The tag is a container and attributes for it follow.
from
Strings that will be searched and replaced with to attribute’s values. Separate multiple values with a comma (or delimiter if changed).
Default: from="" Example: "dog,cat,house"
to
Replacements that will be used to replace from attribute’s values. Comma (or delimiter) separated if multiple.
Default: to="" Example: "ship,home,hat"
delimiter
Sets the delimiter used in from and to to separate multiple values. Default is a comma.
Default: delimiter="," Example: "|"
<rah::replace from="dog" to="cat">
My favorite animal is a dog.
</rah::replace>Returns: My favorite animal is a cat.
<rah::replace from="house,dog,Mike" to="boat,friend,wife">
I live in a house with my dog and Mike.
</rah::replace>Returns: I live in a boat with my friend and wife.
<rah::replace from="Mike,dad" to="I">
I remember when dad and Mike did go to fishing.
</rah::replace>Returns: I remember when I and I did go to fishing.
By default any comma is treated as a delimiter and can not be used as actual value. To use a comma (,) as a needle or a replacement, you would have to change the delimiter to something else. Like for instance to a vertical bar:
<rah::replace from=".|," to="!" delimiter="|">
A, B, C.
</rah::replace>Returns: A! B! C!
- Register the tag for Textpattern >= 4.7.0 compatibility.
- Now requires Textpattern >= 4.7.0.
- The Composer package uses textpattern/installer and textpattern/lock.
- Minor clean up.
- Released as a Composer package.
- Performance optimization.
- Performance optimization.
- Added a new attribute:
delimiter.
- Initial release.