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

preg_replace and smarty

 
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
nosenc
Smarty Rookie


Joined: 30 Sep 2014
Posts: 7

PostPosted: Tue Oct 21, 2014 12:38 pm    Post subject: preg_replace and smarty Reply with quote

Hello there

I got this line:

Code:
<td valign="top" class="edittext" style="background-color:#e2d9d9; border: 1px solid #fff">&nbsp;[{$listitem->oxorderarticles__oxselvariant->value}]</td>


The problem here is that the record contais a comment &lt;!-- randomID --&gt; which is displayed. Now I thought that I could just do something like this:

Code:
[{if $listitem->oxorderarticles__oxselvariant->value}][{$listitem = preg_replace_callback( "/&lt;!--(.|\s)*?--&gt;/", "###", $listitem )}][{/if}]


But it deletes everything which is in the <td>

Any solutions to this?

PS: I have never offended smarty. I just can't use it properly^^


Last edited by nosenc on Tue Oct 21, 2014 2:58 pm; edited 1 time in total
Back to top
View user's profile Send private message
nosenc
Smarty Rookie


Joined: 30 Sep 2014
Posts: 7

PostPosted: Tue Oct 21, 2014 2:57 pm    Post subject: Reply with quote

Used this to see if it works:

Code:
[{ $listitem->oxorderarticles__oxselvariant->value|replace:"&lt;!--":"a"}]


Unfortunately, it does not.

EDIT: I tried the first string in replace with another character and it works o.O

Why would it not work with &lt;!-- ???

What I need is something more like this (just without my noobiness^^): /&lt;!--.*?--&gt;/
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Tue Oct 21, 2014 7:14 pm    Post subject: Reply with quote

Is it the only HTML tag in the text? You could try using |strip_tags modifier to strip all tags. Otherwise make a custom modifier plugin to remove them. Or, remove them in PHP before being sent to the templates.
Back to top
View user's profile Send private message Visit poster's website
nosenc
Smarty Rookie


Joined: 30 Sep 2014
Posts: 7

PostPosted: Wed Oct 22, 2014 7:14 am    Post subject: Reply with quote

The thing is that the following works:

Code:
regex_replace:"/--(.|\s)*?--/":""


But the following won't:

Code:
regex_replace:"/&lt;--(.|\s)*?--&gt;/":""


What I assume is that the code thinks that &lt; and &gt; are special characters ("<" and ">"). Is there a way to say: Hey code, take this literally^^

I will try your suggestion to make a new template, but i don't think that it will work, it would otherwise work with the code I have already written.

The point is that I need this comments for other functions and thats why I have to live with them^^
Back to top
View user's profile Send private message
nosenc
Smarty Rookie


Joined: 30 Sep 2014
Posts: 7

PostPosted: Wed Oct 22, 2014 1:28 pm    Post subject: Reply with quote

Code:
regex_replace:"/lt;.*?gt;/":""|replace:"&":""|replace:"amp;":""


This code did it.

Im suprised that the community here is so shy because many people read this post but would not post anything^^
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Wed Oct 22, 2014 8:51 pm    Post subject: Reply with quote

Your problem was that your variable did not contain the escaped version "&lt;!-- randomID --&gt;" of "<!-- randomID -->".
For some reasion the content of your variable must have been double escaped so that it's content was "&amp;lt;!-- randomID --&amp;gt;"

The following code will work for not escaped, escaped and double escaped content.

Code:
{$foo||regex_replace:"/(<|&(amp;)?lt;).*?(>|&(amp;)?gt;)/":""}
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Fri Nov 21, 2014 12:14 am    Post subject: Reply with quote

The real problem is that someone applied escaping in a wrong place.
There must be only two places where you escape your data:
1. When you the data is stored - to prevent corruption of the data by storage engine. (I.e. mysql_real_escape string and family.)
2. When you transmit data to client - to ensure proper rendering.
If you have corrupted data in any other place or at any other time - you're doing it wrong.
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