|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
gregw Smarty Rookie
Joined: 03 Jan 2017 Posts: 6
|
Posted: Thu Feb 16, 2017 4:12 pm Post subject: Help with Regex_Replace |
|
|
I'm trying to split up a string using smarty, but the system I'm using doesn't support explode: The string looks like:
Code: | text string 1 - text string 2 - text string 3 |
I am using regex_replace to find the FIRST value:
Code: | {$items->title|regex_replace:'/(-.*)/':''} |
Yields: 'text string 1'
I am able to get the LAST value:
Code: | {$items->title|regex_replace:'/(.*-)/':''} |
Yields: 'text string 3'
I am able to get everything BUT the middle value:
Code: | {$items->title|regex_replace:'/(-.*?-)/':''} |
Yields: 'text string 1 text string 3'
How do I use regex_replace to isolate the text on either side of the '-'s so that I can replace them with ''?
Any thoughts will be GREATLY appreciated. |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Thu Feb 16, 2017 5:34 pm Post subject: |
|
|
Why are you doing REPLACE to FIND something?
More importantly…
Why are you doing this in Smarty?
Smarty is not a programming language, it's a presentation language.
Do all data preparation in your PHP code, and assign already prepared datasets to Smarty, ready to display. |
|
Back to top |
|
gregw Smarty Rookie
Joined: 03 Jan 2017 Posts: 6
|
Posted: Thu Feb 16, 2017 5:40 pm Post subject: |
|
|
AnrDaemon wrote: | Why are you doing REPLACE to FIND something?
More importantly…
Why are you doing this in Smarty?
Smarty is not a programming language, it's a presentation language.
Do all data preparation in your PHP code, and assign already prepared datasets to Smarty, ready to display. |
I hear ya, brother. I use an email service provider that allows for smarty tags to be used in their email templates. In this case, I'm parsing values from a poorly formatted RSS feed and inserting into emails using foreach loops, etc.
Here's what I've ended up doing:
first strip the leading value and save as a var:
Code: | {{assign var=title3 value=$items->title|regex_replace:'/(^[^-]*-\s)/':''}} |
Then perform the first regex_replace again on that new var
Code: | {{$title3|regex_replace:'/(\s-.*)/':''}} |
Thanks for looking. |
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Thu Feb 16, 2017 10:12 pm Post subject: |
|
|
So, just to be clear, even though Smarty runs under PHP, this implementation of Smarty does not have access to use any of PHP's functions as a variable modifier?
You said explode() is not available. Perhaps also preg_split().
I think it is really hard to create an array in Smarty, if that was an optional objective.
If you actually do have access to PHP's functions (just not any that create arrays), then...
There is {append}, so maybe cycling through the string would work.
Reverse the string, search for the position of the first dash, use that to truncate the string, reverse the result, append it to the array.
Using the original value, look for the position of the last dash and truncate. This is the new original value.
Repeat. |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Thu Feb 16, 2017 11:17 pm Post subject: |
|
|
bsmither wrote: | So, just to be clear, even though Smarty runs under PHP, this implementation of Smarty does not have access to use any of PHP's functions as a variable modifier? |
What made you think so?
Documentation explicitly says that unless you configure and attach a security class, you can use any PHP function as variable modifier.
F.e.
But being able to doesn't mean you should. |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|