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

KJYe.Name 葉家仁 picture KJYe.Name 葉家仁 · Dec 8, 2010 · Viewed 36.5k times · Source

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

Answer

Jimby picture Jimby · Dec 14, 2010

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