PDA

View Full Version : str_replace and apostrophes


Lynne
03 Dec 2009, 00:28
I wrote a mod to put the breadcrumbs at the bottom of the showthread page. It works just fine except when either the forum title or thread title has an apostrophe in it. Basically, I copied the breadcrumb portion of the navbar and put it in it's own template, render the template, then do a str_replace on it and then get an error if there is an apostrophe in it. I've tried all sorts of things to get this to work (addslashes then stripslashes, not using a template for the breadcrumb code) and just can't get it to work in anyway except to use a template_hook (which would be great except there isn't a template hook where I want one!).

Basic code:
Template, navbar_breadcrumbs, that has (a bunch of stuff, including..):
{vb:raw navbits.breadcrumb}
{vb:raw navbits.lastelement}A plugin:
$newTemplate = vB_Template::create('navbar_breadcrumbs');
$newTemplate->register('navbits', $navbits);
$breadcrumbs = $newTemplate->render();

$find = '<div id="thread_info">';

$vbulletin->templatecache['SHOWTHREAD'] = str_replace($find,$breadcrumbs.$find, $vbulletin->templatecache['SHOWTHREAD']);That doesn't work.

I tried:
$breadcrumbs = addslashes($newTemplate->render()); And, as expected, I get output with quotes escaped.

I've added in stripslashes for the whole preg_replace, for just the 'replace' part, I've done addslashes to each item and then stripslashes to the whole thing. It just isn't coming out right. As I said, I can do a template_hook (and probably will), but I have a feeling it's something real simple and now I'm really curious about how this might work anyway and just wanna know if there is some easy solution I just haven't hit on.

BBR-APBT
03 Dec 2009, 00:34
I think this is how I did it.

$breadcrumbs = strtr($breadcrumbs,"`’","'''");

or

$breadcrumbs = htmlentities($breadcrumbs, ENT_QUOTES);


I can't remember which one fixed my problem with this. I know it was one of them.

Lynne
03 Dec 2009, 00:51
Thanks, I'll try that out!

edit: Nope, neither worked. The first gave me the same parse error I kept getting, and the second gave me the text itself in the page:
<<!-- BEGIN TEMPLATE: navbar_breadcrumbs --> <div class="breadcrumbcontainer" style="margin-bottom:10px"> etc...

Back to being perplexed....

BBR-APBT
03 Dec 2009, 00:53
Your welcome.

Let me know which one works I had this problem so long ago I don't remember which one it was. I luckily remembered the code it was in though.

Lynne
03 Dec 2009, 01:20
Whoops, looks like you replied while I was editing. Unfortunately, neither of those worked. :(

BBR-APBT
03 Dec 2009, 03:21
Ok I got it I went and installed your mod and played till I got it.


It wont let me paste it here, So I had to put it on pastebin.
http://pastebin.com/m1434e916

the forum kept parsing the &/#039/; (added slashes)

Lynne
03 Dec 2009, 04:39
Thank you, thank you, Thank You!!!!

I kept seeing stuff out there about replacing the apostrophe like you did, but kept thinking I wouldn't have to do that. But, I guess that is what has to be done. Thank you very much for helping me out. :D

(and yeah, even copy/paste parsed the line so when I first tried it, it didn't work - then I saw what it had done. :D )

BBR-APBT
03 Dec 2009, 04:50
Your very welcome.

As much as you help me and others its the least I could do.

:D