Question

I'm using smarty template engine to implement my templates for a website.

I have to extract the first word of a varible, e.g. the variable "$a" with the content "hello world foo ..." must be seperated into two variables: "$b" = "hello" and "$c" with the rest (which can be of different size) of "$a", so "world foo ...".

Was it helpful?

Solution

{assign 'tsplit' ' '|explode:'Hello world foo'}
{assign 'a' $tsplit|array_shift}
{assign 'b' $tsplit|array_shift}
{assign 'c' $tsplit|implode:' '}
{$a} - {$b} - {$c}

This could give an unexpected result when you have combined words such as hamster wheel or similar

EDIT

I accidentally misread the question. Here is a decent update

{assign 'a' 'Hello world foo'}
{assign 'tsplit' ' '|explode:$a}
{assign 'b' $tsplit|array_shift}
{assign 'c' $tsplit|implode:' '}
{$a} - {$b} - {$c}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top