megetron Posted April 26, 2012 Share Posted April 26, 2012 hi, I would like to convert a php string like "that's all folks!" to javascript string "that\'s all folks", so I can write something like: diocument.write('<?$myString?>'); so of course i can replace ' with \' with replace function but is there a php function that do this for the specific charachter and for other special charchters too? what is the best way do this? Thanks guys, Quote Link to comment https://forums.phpfreaks.com/topic/261670-how-to-convert-php-string-ro-javascript-string/ Share on other sites More sharing options...
Jessica Posted April 26, 2012 Share Posted April 26, 2012 add_slashes should work Quote Link to comment https://forums.phpfreaks.com/topic/261670-how-to-convert-php-string-ro-javascript-string/#findComment-1340894 Share on other sites More sharing options...
requinix Posted April 27, 2012 Share Posted April 27, 2012 json_encode would be the function you're looking for. Quote Link to comment https://forums.phpfreaks.com/topic/261670-how-to-convert-php-string-ro-javascript-string/#findComment-1340942 Share on other sites More sharing options...
ManiacDan Posted April 27, 2012 Share Posted April 27, 2012 jesi, addslashes only seems to cause problems. It isn't self-aware, and calling it twice often irreparably breaks a string. It should be gone in a not-too-distant-future release of PHP. Quote Link to comment https://forums.phpfreaks.com/topic/261670-how-to-convert-php-string-ro-javascript-string/#findComment-1340951 Share on other sites More sharing options...
Jessica Posted April 27, 2012 Share Posted April 27, 2012 I don't know why you'd call it twice, but ok...I never use it, just popped into my mind for this. Quote Link to comment https://forums.phpfreaks.com/topic/261670-how-to-convert-php-string-ro-javascript-string/#findComment-1340958 Share on other sites More sharing options...
ManiacDan Posted April 27, 2012 Share Posted April 27, 2012 People call it twice because there's no way to tell if it's been called before, and there are built-in features of PHP (or used to be) that did it for you, so people ended up doubling or tripling up on slashes and it was a huge problem. Not so much anymore thanks to magic_quotes being killed, but I still hate it. Quote Link to comment https://forums.phpfreaks.com/topic/261670-how-to-convert-php-string-ro-javascript-string/#findComment-1341099 Share on other sites More sharing options...
Jessica Posted April 27, 2012 Share Posted April 27, 2012 So are you saying that if you called json_encode on an already escaped string, it would not add extra slashes like add slashes would? Quote Link to comment https://forums.phpfreaks.com/topic/261670-how-to-convert-php-string-ro-javascript-string/#findComment-1341101 Share on other sites More sharing options...
ManiacDan Posted April 27, 2012 Share Posted April 27, 2012 Oh no it definitely would, I'm just prejudiced against addslashes because: A) There's always a better solution, addslashes was a hack from back in the day B) Addslashes combined with magic_quotes produced a significant number of threads in the PHP forums while it was still a default, and I got really sick of them Quote Link to comment https://forums.phpfreaks.com/topic/261670-how-to-convert-php-string-ro-javascript-string/#findComment-1341143 Share on other sites More sharing options...
Jessica Posted April 27, 2012 Share Posted April 27, 2012 So what does self-aware mean? Sorry to derail the thread, I'm just trying to figure out what you meant Quote Link to comment https://forums.phpfreaks.com/topic/261670-how-to-convert-php-string-ro-javascript-string/#findComment-1341147 Share on other sites More sharing options...
ManiacDan Posted April 27, 2012 Share Posted April 27, 2012 Some other encoding functions won't step on their own toes (and double-encode things), just in case they're called twice. I thought mysql_real_escape_string wouldn't double-encode things, but requinix (privately) told me I would be wrong if I mentioned that. So ignore that bit, I thought there were other algorithms that behaved more intelligently than they do. Quote Link to comment https://forums.phpfreaks.com/topic/261670-how-to-convert-php-string-ro-javascript-string/#findComment-1341198 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.