Skip to content

Commit

Permalink
Added |replace_last(search, replace) filter
Browse files Browse the repository at this point in the history
  • Loading branch information
mahagr committed Mar 17, 2022
1 parent 010753b commit c083410
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions 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()`

Expand Down
16 changes: 16 additions & 0 deletions system/src/Grav/Common/Twig/Extension/GravExtension.php
Expand Up @@ -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]),
Expand Down Expand Up @@ -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
*
Expand Down

0 comments on commit c083410

Please sign in to comment.