diff --git a/src/Twig/TextExtension.php b/src/Twig/TextExtension.php index f2f86379..7c1e804f 100644 --- a/src/Twig/TextExtension.php +++ b/src/Twig/TextExtension.php @@ -22,6 +22,7 @@ class TextExtension extends AbstractExtension new TwigFilter('safestring', [$this, 'safeString']), new TwigFilter('slug', [$this, 'slug']), new TwigFilter('ucwords', [$this, 'ucwords']), + new TwigFilter('preg_replace', [$this, 'pregReplace']), ]; } @@ -43,4 +44,12 @@ class TextExtension extends AbstractExtension return ucwords($string, $delimiters); } + + /** + * Perform a regular expression search and replace on the given string. + */ + public function pregReplace(string $str, string $pattern, string $replacement = '', int $limit = -1): string + { + return preg_replace($pattern, $replacement, $str, $limit); + } }