From 6fb2f52b4fdcf04080222fe2df442dc4ebaa822e Mon Sep 17 00:00:00 2001 From: Bob den Otter Date: Sat, 1 Feb 2020 16:26:42 +0100 Subject: [PATCH] Add `preg_replace` twig filter --- src/Twig/TextExtension.php | 9 +++++++++ 1 file changed, 9 insertions(+) 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); + } }