Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Help with Regex_Replace

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
gregw
Smarty Rookie


Joined: 03 Jan 2017
Posts: 6

PostPosted: Thu Feb 16, 2017 4:12 pm    Post subject: Help with Regex_Replace Reply with quote

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
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu Feb 16, 2017 5:34 pm    Post subject: Reply with quote

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
View user's profile Send private message
gregw
Smarty Rookie


Joined: 03 Jan 2017
Posts: 6

PostPosted: Thu Feb 16, 2017 5:40 pm    Post subject: Reply with quote

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
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Thu Feb 16, 2017 10:12 pm    Post subject: Reply with quote

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
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu Feb 16, 2017 11:17 pm    Post subject: Reply with quote

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.
Code:
{'c'|date}


But being able to doesn't mean you should.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP