0

Here's my string seperated by commas (,) $myStr = "One, Two,Three, Four , Five ,Six "

I am able successfully explode it {assign var="options" value=","|explode:$myStr}

But I also would like to remove whitespace at the beginning or end from each string when outputting it. In other words: I am looking for a SMARTY function equivalent to PHP's built-in trim($mystr) function. PHP trim will remove the start and end white-spaces if it present, otherwise return the actual string.

{section name=myname loop=$options}
   {$options[myname]}
{/section}

The code above would output:

One
Two
Three
[whitespace]Four[whitespace]
[whitespace]Five[whitespace]
Six[whitespace]

How can I trim whitespaces?

Eldho NewAge
  • 1,313
  • 3
  • 13
  • 17
  • 2
    Duplicate please check: http://stackoverflow.com/questions/1279774/to-strip-whitespaces-inside-a-variable-in-php – RJParikh Apr 14 '16 at 12:09
  • No. Its not duplicate. Both are different questions. My concern is about to remove the white spaces at **start** and **end** positions only. Question which you mentioned is about removing all spaces from a string. – Eldho NewAge Apr 14 '16 at 13:44
  • 1
    I am looking for a **SMARTY** equilent of PHP trim($mystr) function. PHP trim will remove the start and end white-spaces if its present, otherwise return the actual string. – Eldho NewAge Apr 14 '16 at 14:16

2 Answers2

9

There is {strip}{/strip} in smarty. Whitespaces will be removed between theese tags.

{$var|substr:0:-1} would remove last character in your case the whitespace. {$var|substr:1} would remove the first character from string.

all php-functions can be used as modifiers implicitly (more below) and modifiers can be combined

from docs {$var|trim} in smarty is equal to trim($var) in php.

There is another way. Using {php}{/php} tags in in smarty to able to use php built-in trim() function, but that makes no sense to me. Why would you even want to put smarty variable back to php if it was included by php? Nonsense.

  • Thank you for the answer. But it will remove all the spaces with in the string. I want to remove the whitespaces onloy from both ends of a string. – Eldho NewAge Apr 14 '16 at 13:07
  • Thank you again, But it will remove the first and last characters from the string, if white-spaces are not presented at ends. I am looking for a php quilent of trim($mystr). PHP trim will remove the start and end whitespaces if its present, otherwise return the actual string. – Eldho NewAge Apr 14 '16 at 13:49
  • 1
    works for me. just tested. `$smarty->assign('text', ' empty ');` and then in smarty `{$text|trim}` removed the whitespaces. –  Apr 14 '16 at 14:24
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/109172/discussion-between-ksno-and-eldho-newage). –  Apr 14 '16 at 14:46
0

If the white space is just whitespace as in space characters, you can use the function trim() (Docs) to strip it. Replace $options[myname] with trim($options[myname]).

If it's the string "[whitespace]", you can use preg_replace() (Docs) like: preg_replace("/[whitespace]/","",$options[myname])

James Paterson
  • 2,652
  • 3
  • 27
  • 40