diamondnular Posted March 28, 2007 Share Posted March 28, 2007 Hi all, I am trying to write a php code which will output a javascript like this: $text = " <script type=\"text/javascript\"> var FO = "a&b"; .... </script> "; return $text; but when I check the output html code, it becomes: <script type="text/javascript"> var FO = "a&b"; .... </script> It seems that "&" has been changed to "&". How can I prevent this? How can I output just "&" in html code? Thanks, KC Link to comment https://forums.phpfreaks.com/topic/44640-crazy-problem-of/ Share on other sites More sharing options...
neoform Posted March 28, 2007 Share Posted March 28, 2007 i believe php has a built in function that converts it for you (in the php.ini), but what's more likely is your web browser is doing it for you when you view html. FF corrects common html errors that is sees in the code. Link to comment https://forums.phpfreaks.com/topic/44640-crazy-problem-of/#findComment-216773 Share on other sites More sharing options...
diamondnular Posted March 28, 2007 Author Share Posted March 28, 2007 Hi Neoform, Thanks. If the browser does that job, so how can I rewrite the code so that in the html source code, it should be "a&b" not "a&b"? Javascript does not recognize "&" though. KC. Link to comment https://forums.phpfreaks.com/topic/44640-crazy-problem-of/#findComment-216787 Share on other sites More sharing options...
per1os Posted March 28, 2007 Share Posted March 28, 2007 If you are using firefox it would be a setting particular to firefox. I would check it with IE by viewing the source. I really do not like that aspect of firefox although it can be handy sometimes. What browser are you using to view the source? Link to comment https://forums.phpfreaks.com/topic/44640-crazy-problem-of/#findComment-216793 Share on other sites More sharing options...
diamondnular Posted March 28, 2007 Author Share Posted March 28, 2007 Hi, I just used IE to check the code, and even source code is "a&b", IE still reads the code just fine, and the code works in IE. But it seems that FF does not read the code as "a&b" then javascript does not work! It is so strange! Anybody has idea to overcome this problem? Bests, KC. Link to comment https://forums.phpfreaks.com/topic/44640-crazy-problem-of/#findComment-216795 Share on other sites More sharing options...
per1os Posted March 28, 2007 Share Posted March 28, 2007 http://us3.php.net/manual/en/function.html-entity-decode.php Maybe that will help? Link to comment https://forums.phpfreaks.com/topic/44640-crazy-problem-of/#findComment-216802 Share on other sites More sharing options...
shaunrigby Posted March 28, 2007 Share Posted March 28, 2007 why dont you just type and in instead? Link to comment https://forums.phpfreaks.com/topic/44640-crazy-problem-of/#findComment-216854 Share on other sites More sharing options...
kenrbnsn Posted March 28, 2007 Share Posted March 28, 2007 You have some unescaped double quotes in your original sample. Try something like this: <?php $text = ' <script type="text/javascript"> var FO = "a&b"; .... </script> '; return $text; ?> Ken Link to comment https://forums.phpfreaks.com/topic/44640-crazy-problem-of/#findComment-216918 Share on other sites More sharing options...
diamondnular Posted March 28, 2007 Author Share Posted March 28, 2007 You have some unescaped double quotes in your original sample. Try something like this: Ken Yes Ken, I had some typo when typing the question. But inside my real code, it was written correctlY: $text = " <script type=\"text/javascript\"> var FO = \"a&b\"; .... </script> "; return $text; Link to comment https://forums.phpfreaks.com/topic/44640-crazy-problem-of/#findComment-216944 Share on other sites More sharing options...
poirot Posted March 28, 2007 Share Posted March 28, 2007 Yes Ken, I had some typo when typing the question. But inside my real code, it was written correctlY: Ken meant to replace your double quotes with single quotes. It might work - as it seems like PHP is somehow replacing '&' with the HTML entity. Link to comment https://forums.phpfreaks.com/topic/44640-crazy-problem-of/#findComment-216949 Share on other sites More sharing options...
diamondnular Posted March 28, 2007 Author Share Posted March 28, 2007 I finally came up with solution by using javascript unescape function, then I can avoid annoying "&": $text = " <script type=\"text/javascript\"> var FO = \"a\" + unescape('%26') + \"b\"; .... </script> "; return $text; Thanks everyone. KC. Link to comment https://forums.phpfreaks.com/topic/44640-crazy-problem-of/#findComment-216952 Share on other sites More sharing options...
diamondnular Posted March 28, 2007 Author Share Posted March 28, 2007 Yes Ken, I had some typo when typing the question. But inside my real code, it was written correctlY: Ken meant to replace your double quotes with single quotes. It might work - as it seems like PHP is somehow replacing '&' with the HTML entity. Hi Poirot, I understood what Ken meant anyway, because before posing question here, my original code was wrapped with single quote and it did not work, as "&" is always change to "&" no matter what kind of quote you use. KC Link to comment https://forums.phpfreaks.com/topic/44640-crazy-problem-of/#findComment-216957 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.