Jump to content

Replace quotation marks in a string with another character?


Punk Rock Geek

Recommended Posts

I have a strange problem.  While I know how to replace quotation marks with another character, for some reason it no longer works if the string was created using data from an input form.

 

Here is the start of my code.  The first variable was created through an html form.  The second one was created without a form:

 

$formvariable = stripslashes($formvariable);
$normalvariable = '"';

 

If I echo $formvariable and $normalvariable at this line, they both return a double quotation mark.  They are identical.  My code continues:

 

 

$formvariable = str_replace('"',"", $formvariable);
$normalvariable = str_replace('"',"", $normalvariable);

 

 

Now when I echo them both, the normalvariable's quotation mark gets replaced, while the formvariable stays the same.  Even though they were both the same and they both had the same thing done to them.

 

Anyone have any idea of what could be happening?  ???

It could be that the double quote coming from your form is actually a different double quote character with a different character code value.  Try using the ord function to output each of the individual character codes to see if they match.

 

http://www.php.net/ord

It could be that the double quote coming from your form is actually a different double quote character with a different character code value.  Try using the ord function to output each of the individual character codes to see if they match.

 

http://www.php.net/ord

 

Thanks.  It turns out that they don't match.  The formvariable is a 38 and the normalvariable is a 34.  No idea of how to fix this though?

If you look at an ASCII table, you can see that 34 is a double quote, and 38 actually is an ampersand (ord() only checks the first character of the input string). So I'm guessing the double quotes in $formvariable actually are HTML entities; &#38 - representing a double quote. When you output the two variables, did you look at the source code? Always do that to be sure exactly what's output.

 

$formvariable = str_replace('&', '', $formvariable);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.