Search code examples
prestashopsmartystrsplit

Smarty equivalent for str_split()


Im looking for an equivalent for str_split() in Smarty as {php}{/php} looks like deprecated and not very clean. I came across explode but it needs a delimiter and what I'm trying to get is:

1/2/3/4/5

from

$chars = str_split(12345);
foreach($chars as $char){
    echo $char . '/';
}

But in a Smarty environment. Any idea please?


Solution

  • Here is a regex based approach:

    $chars = "12345";
    $output = preg_replace("/(?<=\d)(?=\d)/", "/", $chars);
    echo $output;  // 1/2/3/4/5