str_replace doesn't seem to work on any symbols insanity

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lemmy101
Forum Newbie
Posts: 4
Joined: Fri Oct 12, 2007 9:34 pm

str_replace doesn't seem to work on any symbols insanity

Post by lemmy101 »

Hi all, I'm having some crazy problems with str_replace (and all similar functions) I can't understand.

Basically, I have a string with HTML, and I'm trying to alter it with str_replace before serving it to the browser...

$content = str_replace("accesskey=\"x\"", "accesskey=\"x\" value=\"Preview\"', $content) ;

Okay, so I want to find the HTML tag that has the accesskey="x" tag and append a value tag to it. However, try as I might, the str_replace seems to do nothing.

HOWEVER if I do this:

$content = str_replace("accesskey=\"x\"", "a", $content) ;

it ends up with:

a=""

??? :(

This is really baffling me, I've tried all the str_replace variants, I've tried using 'accesskey="x"', I've tried:

$content = str_replace("accesskey", "value=\"Preview\" accesskey", $content) ;

I've tried:

$content = str_replace("accesskey", "value=\'Preview\' accesskey", $content) ;

It just seems that if an =, ", ', ;, : or any other symbol is in either of the strings it fails to do anything... I've looked at the string in a debugger and it does indeed have the escape characters: \" but I've tried it without regardless.

I can't use htmlspecialchars etc because I need it to be parsed as html when it's echoed to the screen.

Please help :-S I've been hacking away at this for literally 10 hours+ and can only imagine I'm doing something ridiculously thick for seemingly no one on the entire internet has had the same problem if my 1000s of google searches are anything to go by :(

Sorry for the ranty post, and any help would be much appreciated!

Thanks,

lemmy
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

str_replace() requires fairly accurate, as in binary, similarity for a replacement to happen. i.e. it is case-sensitive and you may also be mismatching whitespace characters.
lemmy101
Forum Newbie
Posts: 4
Joined: Fri Oct 12, 2007 9:34 pm

Post by lemmy101 »

Hi thanks for the feedback, thing is I'm pasting this directly from the output, I've also tried using the stri_replace to no effect.

Thanks!

lemmy
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Try using var_dump() to compare the various strings.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

Just a little tip, try simplifying your String.

Code: Select all

$content = str_replace("accesskey=\"x\"", "accesskey=\"x\" value=\"Preview\"', $content);
->

Code: Select all

$content = str_replace('accesskey="x"', 'accesskey="x" value="Preview"', $content);
Typing this up, i found you had a Syntax error. For the Replace part you opened it with a double quote, and tried closing it with a single quote.
Notice how $content on the first snip it is red? Php thinks its part of the string. Do you have error_reporting on?

Since you aren't using the Functionality of the double quotes why use them?
lemmy101
Forum Newbie
Posts: 4
Joined: Fri Oct 12, 2007 9:34 pm

Post by lemmy101 »

Damn sorry, that was a typo. I'm using PhpEd. it was present in the php when I pasted it into the post. :-S but it's because I've been back and forward between:

Code: Select all

$content = str_replace("accesskey=\"x\"", "accesskey=\"x\" value=\"Preview\"", $content);
and

Code: Select all

$content = str_replace('accesskey="x"', 'accesskey="x" value="Preview"', $content);
So many times since I started trying to find this out :'(

Also I tried doing a var dump and it just re-renders the page again.

The salient contents of the HTML page are there::

Code: Select all

<input type="submit" class="button" accesskey="s" title="(Alt + S)" name="sbutton" tabindex="2" id="qc_submit" onclick="clickedelm = this.value" />
<input type="submit" class="button" accesskey="x" title="(Alt + X)" name="preview" tabindex="3" id="qc_preview" onclick="clickedelm = this.value" />
I've tried copy and pasting that directly into the string, and adding escape characters, or putting in single quotes with no escape characters. PhpEd debugger also contains the following in the watch window:

Code: Select all

<input type=\"hidden\" name=\"lastcomment\" value=\"1191787116\" />\r\n\r\n\t\t\t\t\t\t<input type=\"submit\" class=\"button\" accesskey=\"s\" title=\"(Alt + S)\" name=\"sbutton\" tabindex=\"2\" id=\"qc_submit\" onclick=\"clickedelm = this.value\" />
Though it seems to be adding escape characters itself for copy paste purposes, I tried doing:

Code: Select all

$content = str_replace("accesskey=\\\"x\\\"", "accesskey=\\\"x\\\" value=\\\"Preview\\\"", $content);
No dice.

Now onto the interesting bit. If I actually reset the contents of $content with what the relevant bit I paste in from the watch, output window or Firefox source before I do the str_replace:
i.e.

Code: Select all

$content = '<input type="submit" class="button" accesskey="x" title="(Alt + X)" name="preview" tabindex="3" id="qc_preview" onclick="clickedelm = this.value" />';

$content = str_replace("accesskey=\"x\"", "accesskey=\"x\" value=\"Preview\"", $content);
... then it works. Which either means that from copying or pasting it some of the ASCII codes are changing to make it fail, or that something before it in the string (i.e. the rest of the webpage) is stopping it from working.

Also bearing in mind that in my first example str_replace is actually behaving in a completly incorrect way:

Another example is:

Code: Select all

$content = str_replace("accesskey=\"x\"", "accesskeyvalue", $content);
changes:

Code: Select all

accesskey="x"
into

Code: Select all

accesskeyvalue=""
This to me seems like a serious bug and something that perhaps points to the problem I'm having? (this happens with single quotes too)

Thanks again!

lemmy
lemmy101
Forum Newbie
Posts: 4
Joined: Fri Oct 12, 2007 9:34 pm

Post by lemmy101 »

I fixed it! Hurrah!

For anyone else who finds this as a google search, the problem lies in the database that is serving the string. It's encoding is not in utf-8, and the php file that's doing the str_replace, and therefore the string in the php file is...

I added utf8_encode around all the relevant strings:

Code: Select all

$content = str_replace(utf8_encode('value="" accesskey="x"'), utf8_encode('accesskey="x" value="Preview"'),utf8_encode($content)); 
$content = str_replace(utf8_encode('value="" accesskey="s"'), utf8_encode('accesskey="s" value="Submit"'),utf8_encode($content));
AND IT WORKS! BY CRIKEY! :D

Thanks all for your help, I hope this helps someone else in the future :)

lemmy
Post Reply