PDA

View Full Version : Problem with str_replace function when removal apart of the template


omardealo
07-24-2014, 02:10 AM
Hello ,

i need to removal apart of the template SHOWTHREAD ,
i use str_replace function ,
every thing is okay and function working good ,
but my problem with "Escaping special characters and conditional execution of code"

this the code i want removal it from a template

<if condition="$show['threadrating']">
<td class="vbmenu_control" id="threadrating" nowrap="nowrap">
<a href="$show[nojs_link]#goto_threadrating"><span id="threadrating_current"><if condition="$show['rating']">$vbphrase[rating]: <img class="inlineimg" src="$stylevar[imgdir_rating]/rating_$thread[rating].gif" alt="<phrase 1="$thread[votenum]" 2="$thread[voteavg]">$vbphrase[thread_rating_x_votes_y_average]</phrase>" border="0" /><else />$vbphrase[rate_thread]</if></span></a>
<if condition="$show['popups']"><script type="text/javascript"> vbmenu_register("threadrating"); </script></if>
</td>
</if>


this is my plugin

$code =
"<if condition=\"$show['threadrating']\">
<td class=\"vbmenu_control\" id=\"threadrating\" nowrap=\"nowrap\">
<a href=\"$show[nojs_link]#goto_threadrating\"><span id=\"threadrating_current\"><if condition=\"$show['rating']\">$vbphrase[rating]: <img class=\"inlineimg\" src=\"$stylevar[imgdir_rating]/rating_$thread[rating].gif\" alt=\"<phrase 1=\"$thread[votenum]\" 2=\"$thread[voteavg]\">$vbphrase[thread_rating_x_votes_y_average]</phrase>\" border=\"0\" /><else />$vbphrase[rate_thread]</if></span></a>
<if condition=\"$show['popups']\"><script type=\"text/javascript\"> vbmenu_register(\"threadrating\"); </script></if>
</td>
</if>";

$vbulletin->templatecache['SHOWTHREAD'] = str_replace ($code,' ',$vbulletin->templatecache['SHOWTHREAD']);

vBNinja
07-24-2014, 02:34 AM
Why not just make a plugin in showthread_complete or something with:

$show['threadrating'] = false;

And maybe wrap it around an if statement so it only affects the threads/forums you need it removed in

kh99
07-24-2014, 12:16 PM
If you still want to do the str_replace, I think you'd also need to escape the dollar sign ($) with a backslash. Or it might be easier to make it a single-quoted string and just escape the single quote characters.

Dave
07-24-2014, 12:55 PM
This is a nicer way:

$code =
'<if condition="' . $show['threadrating'] . '">
<td class="vbmenu_control" id="threadrating" nowrap="nowrap">
<a href="' . $show[nojs_link] . '#goto_threadrating"><span id="threadrating_current"><if condition="' . $show['rating'] . '">' . $vbphrase[rating] . ': <img class="inlineimg" src="' . $stylevar[imgdir_rating] . '/rating_' . $thread[rating].gif . '" alt="<phrase 1="' . $thread[votenum] . '" 2="' . $thread[voteavg] . '">' . $vbphrase[thread_rating_x_votes_y_average] . '</phrase>" border="0" /><else />' . $vbphrase[rate_thread] . '</if></span></a>
<if condition="' . $show['popups'] . '"><script type="text/javascript"> vbmenu_register("threadrating"); </script></if>
</td>
</if>';

$vbulletin->templatecache['SHOWTHREAD'] = str_replace ($code,' ',$vbulletin->templatecache['SHOWTHREAD']);

Put the code within single quotes, escape the variables and no need to add slashes in front of quotes.
Also much easier to read.

kh99
07-24-2014, 01:01 PM
This is a nicer way:

$code =
'<if condition="' . $show['threadrating'] . '">
<td class="vbmenu_control" id="threadrating" nowrap="nowrap">
<a href="' . $show[nojs_link] . '#goto_threadrating"><span id="threadrating_current"><if condition="' . $show['rating'] . '">' . $vbphrase[rating] . ': <img class="inlineimg" src="' . $stylevar[imgdir_rating] . '/rating_' . $thread[rating].gif . '" alt="<phrase 1="' . $thread[votenum] . '" 2="' . $thread[voteavg] . '">' . $vbphrase[thread_rating_x_votes_y_average] . '</phrase>" border="0" /><else />' . $vbphrase[rate_thread] . '</if></span></a>
<if condition="' . $show['popups'] . '"><script type="text/javascript"> vbmenu_register("threadrating"); </script></if>
</td>
</if>';

$vbulletin->templatecache['SHOWTHREAD'] = str_replace ($code,' ',$vbulletin->templatecache['SHOWTHREAD']);

Put the code within single quotes, escape the variables and no need to add slashes in front of quotes.
Also much easier to read.


But that won't work the same. You want to avoid having the variables evaluated because you're trying to match the string that's in the template cache.

Dave
07-24-2014, 01:25 PM
But that won't work the same. You want to avoid having the variables evaluated because you're trying to match the string that's in the template cache.

Oh in that case I misunderstood OP.

omardealo
07-25-2014, 06:42 AM
Why not just make a plugin in showthread_complete or something with:

$show['threadrating'] = false;

And maybe wrap it around an if statement so it only affects the threads/forums you need it removed in

becouse i still want threadrating is runing , i try to removal this code and put it under the first post , Thnx

If you still want to do the str_replace, I think you'd also need to escape the dollar sign ($) with a backslash. Or it might be easier to make it a single-quoted string and just escape the single quote characters.

okay brother , i try both but not working .


{escape the dollar sign ($) + escape double quote } : not working

$code =
"<if condition=\"\$show['threadrating']\">
<td class=\"vbmenu_control\" id=\"threadrating\" nowrap=\"nowrap\">
<a href=\"\$show[nojs_link]#goto_threadrating\"><span id=\"threadrating_current\"><if condition=\"\$show['rating']\">\$vbphrase[rating]: <img class=\"inlineimg\" src=\"\$stylevar[imgdir_rating]/rating_\$thread[rating].gif\" alt=\"<phrase 1=\"\$thread[votenum]\" 2=\"\$thread[voteavg]\">\$vbphrase[thread_rating_x_votes_y_average]</phrase>\" border=\"0\" /><else />\$vbphrase[rate_thread]</if></span></a>
<if condition=\"\$show['popups']\"><script type=\"text/javascript\"> vbmenu_register(\"threadrating\"); </script></if>
</td>
</if>";



escape only single quote i try it already : not working

$code =
'<if condition="$show[\'threadrating\']">
<td class="vbmenu_control" id="threadrating" nowrap="nowrap">
<a href="$show[nojs_link]#goto_threadrating"><span id="threadrating_current"><if condition="$show[\'rating\']">$vbphrase[rating]: <img class="inlineimg" src="$stylevar[imgdir_rating]/rating_$thread[rating].gif" alt="<phrase 1="$thread[votenum]" 2="$thread[voteavg]">$vbphrase[thread_rating_x_votes_y_average]</phrase>" border="0" /><else />$vbphrase[rate_thread]</if></span></a>
<if condition="$show[\'popups\']"><script type="text/javascript"> vbmenu_register("threadrating"); </script></if>
</td>
</if>';

--------------- Added 1406277258 at 1406277258 ---------------

But this working
single-quoted string and escape the double quote characters

like that :

$code = '<td class=\"vbmenu_control\" id=\"threadrating\" nowrap=\"nowrap\">';
$vbulletin->templatecache['SHOWTHREAD'] = str_replace ($code,' ',$vbulletin->templatecache['SHOWTHREAD']);


replace <td> Is this enough ? it removed the popup of Thread Rating as well , and i think a problem in Escaping conditional , and this line

<script type="text/javascript"> vbmenu_register("threadrating"); </script>