This plugin adds PHP's preg_replace function as a Twig filter.
Usage
{{ entry.heading|preg_replace('~pattern~', 'replacement') }}
Any backslashes in the regular expression must to be double-escaped \\
to work properly.
How is this different from Craft's replace filter?
In short, the preg_replace filter offers a more direct call to PHP's preg_replace
function.
Craft's filter allows for both normal string and regex replacements but slightly limits the regex replacement functionality.
Example 1
preg_replace allows you to use any delimiter, while replace works only with forward slashes /
.
{{ entry.heading|preg_replace('~pattern~', 'replacement') }}
Example 2
The replace filter performs str_replace
when an array is passed and always ignores the $limit
parameter.
preg_replace binds all it's parameters to PHP's function.
This allows you to pass arrays for the $pattern
and $replacement
parameters, and an integer for the $limit
parameter.
{{ entry.heading|preg_replace(['~pattern1~', '~pattern2~'], ['replacement1', 'replacement2'], 3) }}
To install this plugin, copy the command above to your terminal.
This plugin doesn't have any reviews.