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

Smarty uses preg_replace incorrectly

 
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 -> Bugs
View previous topic :: View next topic  
Author Message
jeteon
Smarty n00b


Joined: 07 Oct 2015
Posts: 2

PostPosted: Wed Oct 07, 2015 6:46 pm    Post subject: Smarty uses preg_replace incorrectly Reply with quote

I'm posting this here because the issue tracker on Google code is no longer operational due to Google code being shut down.

I am using Smarty 3.1 with PHP 5.5.9 on an i386 machine running Ubuntu 14.04. When I used the smarty preg_replace() function, code that has the pattern:
Code:

     {$variable | preg_replace:"/(?i)<br\s*\/?>/":" "}

is translated to something like:
Code:

     <?php echo preg_replace(
             $_smarty_tpl->tpl_vars['variable'],
             "/(?i)<br\s*\/?>/",
             " "
     );?>


According to the PHP documentation, the regexp comes first and the text itself last, instead of the order that is created by Smarty.

jeteon
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed Oct 07, 2015 9:30 pm    Post subject: Reply with quote

It is not Smarty, it is YOU using it incorrectly.
Correct use would be
{$pattern|preg_replace:$replacement:$variable}
Back to top
View user's profile Send private message
jeteon
Smarty n00b


Joined: 07 Oct 2015
Posts: 2

PostPosted: Wed Oct 07, 2015 10:26 pm    Post subject: Reply with quote

Embarassed You're right. I was looking up the syntax for regexp_replace in the docs and testing the regex using preg_replace on the command line. I guess at some point, I carried the function name over into my source.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu Oct 08, 2015 11:05 am    Post subject: Reply with quote

Generally speaking, it is not nice to make complex changes to the passed data in such a way inside the template.
This makes debugging more complicated for the designer.
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Fri Oct 09, 2015 3:25 am    Post subject: Reply with quote

See the regex_replace modifier
http://www.smarty.net/docs/en/language.modifier.regex.replace.tpl
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 -> Bugs 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