Forum Moderators: open

Message Too Old, No Replies

JS " ' error

         

andrewsmd

6:03 pm on Dec 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a function that I need to output html with both " and '. Do you have any idea on how I can do this. If I try
function("output here");
then anything with a " will throw an error. If I try function('output here'); then anything with a ' will throw an error. I know you can escape these characters, but this data is coming from a database and I can't seem to identify the ' or ". Using the regular ascii codes and even " or ' is not recognizing them to replace them with the escaped character. Thanks,

rocknbil

8:24 pm on Dec 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Probably need specific examples. :-)

If it's coming from a database, use your php functions or, being the old schooler I am and like to see what it's doing, preg_replace. The double quotes are easy.

$content = preg_replace('/"/','"',$content);

So not you only have to worry about ' . if you use double quotes in your JS. no sweat, you shouldn't need to do anything at all. Otherwise, escape them:

$content = preg_replace("/'/","\\\\'",$content);

Memory's a little fuzzy on the backslash in PHP, which is why there's four there, you'll need to play with it. To properly get your output to have a literal backslash, like \' that, I recall having to use 4 - but this could be wrong.