Add preg_replace twig filter

This commit is contained in:
Bob den Otter
2020-02-01 16:26:42 +01:00
parent 4d66df9ad6
commit 6fb2f52b4f

View File

@@ -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);
}
}