SMARTY - Keyword in string after the 4th word then add remaining words from the truncated string from the 4th word

222 views Asked by At

I have a sting:

$string = "this string is a lorem ipsum string"

after the 4th word I want to add a keyword like:

$keyword = "newword"

now the result should look like this

$newstring = "this string is a newword lorem ipsum string"

how can i handle this with smarty in a tpl-file?

1

There are 1 answers

0
AbraCadaver On

I haven't used Smarty in a long time, but you can do this in PHP and inject into the template or use the PHP in the template:

{php}
$array  = explode(' ', $string);
$result = implode(' ', array_merge(array_splice($array, 0, 4), [$keyword], $array));
{/php}
  • Explode on spaces
  • Slice out the first 4 words
  • Merge the slice, keyword and original array (minus the slice)
  • Implode on a space