Page 1 of 1

smarty regex_replace [SOLVED]

Posted: Sun Feb 28, 2010 7:54 pm
by antosha
Hi,
I have a little smarty question, hopefully someone can help me out : )

I have many strings that follow the following scheme :

Espiritu Santo, Vanuatu (SON)
Hamilton Island, Australia (HTI)
Bangkok, Thailand (BKK)
New York, NY, United States (NYC)
...


If {$mystring} is Bangkok, Thailand (BKK) how do I make the output to show :
BKK
?

If {$mystring} is New York, NY, United States (NYC) how do I make the output to show :
NYC
?

... ,etc

I hope it's not too confusing  :)
Thanks

Re: smarty regex_replace

Posted: Sun Feb 28, 2010 9:27 pm
by Nullig
Have you tried substr?

{$mystring|substr:-4:3}

Nullig

Re: smarty regex_replace

Posted: Sun Feb 28, 2010 9:30 pm
by Peciura

Code: Select all

{assign var='test' value='New York, NY, United States (NYC)'}
{$test|regex_replace:'/^.*\(|\)/':''}
Here are good links to try and learn some regex
http://www.regular-expressions.info/jav ... ample.html
http://www.regular-expressions.info/reference.html

Re: smarty regex_replace

Posted: Sun Feb 28, 2010 9:52 pm
by antosha
Thanks a lot, both solutions work great :)
@ Nulling: I didn't know "substring" modifier existed in smarty, thanks.
@ Peciura: thanks for regex documentation, that will help me a lot.