smarty regex_replace [SOLVED]

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Post Reply
antosha
Forum Members
Forum Members
Posts: 60
Joined: Fri Jun 06, 2008 7:20 pm
Location: Montreal, Canada

smarty regex_replace [SOLVED]

Post 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
Last edited by antosha on Sun Feb 28, 2010 9:56 pm, edited 1 time in total.
Follow me on twitter: end_tone
Linked In: levchenkoanton
User avatar
Nullig
Power Poster
Power Poster
Posts: 2380
Joined: Fri Feb 02, 2007 4:31 pm
Location: Comox Valley, BC

Re: smarty regex_replace

Post by Nullig »

Have you tried substr?

{$mystring|substr:-4:3}

Nullig
Peciura

Re: smarty regex_replace

Post 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
antosha
Forum Members
Forum Members
Posts: 60
Joined: Fri Jun 06, 2008 7:20 pm
Location: Montreal, Canada

Re: smarty regex_replace

Post 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.
Follow me on twitter: end_tone
Linked In: levchenkoanton
Post Reply

Return to “Developers Discussion”