Search code examples
phpsmartysmarty2

Smarty getting substring of a var with strpos as start and strlen as end


I am having issue formatting variables in smarty. I was wondering what is the best way to do it. Basically i have a string "ABC | DEFGH" i want smarty to get the substring of "DEFGH" How would i go about doing this?

{$var|substr:strpos:"|":strlen}

doesn't work


Solution

  • Just solved this without setting var back in PHP, and by using the built-in function wrappers.

    Assuming that: $var = "ABC|DEFGH";

    {assign var="bar_at" value=$var|strpos:"|"}
    <li>{$var}</li>
    <li>{$var|substr:0:$bar_at}</li>
    <li>{$var|substr:$bar_at+1}</li>
    

    This will print:

  • ABC|DEFGH
  • ABC
  • DEFGH