diff --git a/CHANGELOG.md b/CHANGELOG.md index 6808460ccf..b77eabea83 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # v1.7.32 ## mm/dd/2022 +1. [](#new) + * Added `|replace_last(search, replace)` filter 1. [](#improved) * Added multi-language support for page routes in `Utils::url()` diff --git a/system/src/Grav/Common/Twig/Extension/GravExtension.php b/system/src/Grav/Common/Twig/Extension/GravExtension.php index 52e2a1841b..d46b28f084 100644 --- a/system/src/Grav/Common/Twig/Extension/GravExtension.php +++ b/system/src/Grav/Common/Twig/Extension/GravExtension.php @@ -145,6 +145,7 @@ public function getFilters(): array new TwigFilter('yaml_encode', [$this, 'yamlEncodeFilter']), new TwigFilter('yaml_decode', [$this, 'yamlDecodeFilter']), new TwigFilter('nicecron', [$this, 'niceCronFilter']), + new TwigFilter('replace_last', [$this, 'replaceLastFilter']), // Translations new TwigFilter('t', [$this, 'translate'], ['needs_environment' => true]), @@ -547,6 +548,21 @@ public function niceCronFilter($at) return $cron->getText('en'); } + /** + * @param $str + * @param $search + * @param $replace + * @return string|mixed + */ + public function replaceLastFilter($str, $search, $replace) + { + if (is_string($str) && ($pos = mb_strrpos($str, $search)) !== false) { + $str = substr_replace($str, $replace, $pos, mb_strlen($search)); + } + + return $str; + } + /** * Get Cron object for a crontab 'at' format *