Вопрос

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 ...".

Это было полезно?

Решение

{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}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top