Henk Z Henk Z - 4 months ago 28
Smarty Question

php line Smarty template engine reverse stristr

I want to edit my php template, but I can not get it right.
I use the the Smarty template engine.

My default php line looks like this:

{$childItem->getLabel()}


That does display HTML like this:

MY TEXT - MY SUB TEXT


I want to remove all content before the
-
. So that it only displays the
MY SUB TEXT
part.

I tried this, but that does not work, because that removes the content after the sign:

{$childItem->getLabel()|stristr:'-' : true}


How can I fix this?

Answer

The additional "true" parameter you're passing to stristr refers to $before_needle, so you'll get back the portion of the string before the hyphen. Unfortunately, removing that will get you back the portion of the string after and including the hyphen, i.e. "- MY SUB TEXT".

A clean solution would be to use explode, and then use the second element of the resulting array, like this:

{assign var="splitItem" value=" - "|explode:$childItem->getLabel()}
{$splitItem[1]}
Comments