View previous topic :: View next topic |
Author |
Message |
nosenc Smarty Rookie
Joined: 30 Sep 2014 Posts: 7
|
Posted: Tue Oct 21, 2014 12:38 pm Post subject: preg_replace and smarty |
|
|
Hello there
I got this line:
Code: | <td valign="top" class="edittext" style="background-color:#e2d9d9; border: 1px solid #fff"> [{$listitem->oxorderarticles__oxselvariant->value}]</td> |
The problem here is that the record contais a comment <!-- randomID --> which is displayed. Now I thought that I could just do something like this:
Code: | [{if $listitem->oxorderarticles__oxselvariant->value}][{$listitem = preg_replace_callback( "/<!--(.|\s)*?-->/", "###", $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 |
|
nosenc Smarty Rookie
Joined: 30 Sep 2014 Posts: 7
|
Posted: Tue Oct 21, 2014 2:57 pm Post subject: |
|
|
Used this to see if it works:
Code: | [{ $listitem->oxorderarticles__oxselvariant->value|replace:"<!--":"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 <!-- ???
What I need is something more like this (just without my noobiness^^): /<!--.*?-->/ |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Oct 21, 2014 7:14 pm Post subject: |
|
|
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 |
|
nosenc Smarty Rookie
Joined: 30 Sep 2014 Posts: 7
|
Posted: Wed Oct 22, 2014 7:14 am Post subject: |
|
|
The thing is that the following works:
Code: | regex_replace:"/--(.|\s)*?--/":"" |
But the following won't:
Code: | regex_replace:"/<--(.|\s)*?-->/":"" |
What I assume is that the code thinks that < and > 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 |
|
nosenc Smarty Rookie
Joined: 30 Sep 2014 Posts: 7
|
Posted: Wed Oct 22, 2014 1:28 pm Post subject: |
|
|
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 |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Wed Oct 22, 2014 8:51 pm Post subject: |
|
|
Your problem was that your variable did not contain the escaped version "<!-- randomID -->" of "<!-- randomID -->".
For some reasion the content of your variable must have been double escaped so that it's content was "&lt;!-- randomID --&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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Fri Nov 21, 2014 12:14 am Post subject: |
|
|
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 |
|
|