Added |replace_last(search, replace) filter

This commit is contained in:
Matias Griese 2022-03-17 12:15:05 +02:00
parent 010753bdd6
commit c08341046b
2 changed files with 18 additions and 0 deletions

View File

@ -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()`

View File

@ -145,6 +145,7 @@ class GravExtension extends AbstractExtension implements GlobalsInterface
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 @@ class GravExtension extends AbstractExtension implements GlobalsInterface
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
*