unifiedac Posted January 30, 2012 Share Posted January 30, 2012 Hello, Let me explain that title, it's kinda involved. I am attempting to generate a dynamic keyword for the Amazon MP3 Widget. No matter which page you're on, the PHP variable $keyword will contain a string. That string is being used within the widget's Javascript code. Here is the part of the code that matters: <script type='text/javascript'> amzn_wdgt.title='<?php echo $keyword; ?>'; </script> Everything works fine, meaning the keyword is appropriately transferred into the script. However, not all strings are displaying correctly. As you can see in the example below, the widget in the top right corner is not appropriately translating the apostrophe. When the variable is echoed outside the Javascript, there is no problem, the apostrophe displays appropriately. It only occurs when the variable is echoed inside the Javascript: Outside Javascript Beethoven's Trinklied No. 282 Inside Javascript Beethoven’s Trinklied No. 282 Here is the live site. The widget is in the top right of the sidebar. It is the widget title that is in question: http://www.drinking-songs.com/beethovens-trinklied-no-282 I have applied various ASCII decode functions, addslashes, etc. to the variable, but nothing seems to work. I have a feeling there is something within the Javascript knowledgebase that I'm not aware of. Any help is appreciated. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/256053-special-characters-from-php-variable-within-javascript/ Share on other sites More sharing options...
Adam Posted January 30, 2012 Share Posted January 30, 2012 Those are HTML entities. Flash is not HTML, so you need to decode them. You could pass the string through html_entity_decode: amzn_wdgt.title='<?php echo html_entity_decode($keyword); ?>'; Ideally though you should find where the string is originally converted to entities (either with htmlentities or htmlspecialchars) and remove the escaping there. Also, removing the entity conversion would mean that the single quote would now break the JS syntax, so I would pass the string through json_encode without the surrounding quotes: amzn_wdgt.title=<?php echo json_encode($keyword); ?>; That will result in escaped notation that JS can understand. Don't forget to remove the HTML entity escaping though. Quote Link to comment https://forums.phpfreaks.com/topic/256053-special-characters-from-php-variable-within-javascript/#findComment-1312659 Share on other sites More sharing options...
unifiedac Posted January 30, 2012 Author Share Posted January 30, 2012 Thanks for the reply, but it doesn't seem to work. This is what I did. First I applied the HTML entity decode function: <?php $keyword = html_entity_decode($keyword); ?> Then I applied the json encode within the Javascript: amzn_wdgt.title=<?php echo json_encode($keyword); ?>; As you can see, it still appears with the special characters: http://www.drinking-songs.com/beethovens-trinklied-no-282 Quote Link to comment https://forums.phpfreaks.com/topic/256053-special-characters-from-php-variable-within-javascript/#findComment-1312692 Share on other sites More sharing options...
Adam Posted January 30, 2012 Share Posted January 30, 2012 The entities have been double encoded by the look of it. First it was: Beethoven’s Trinklied No. 282 Now it's: "Beethoven’s Trinklied No. 282"; So a second time through html_entity_decode() should produce: Beethoven’s Trinklied No. 282 I strongly urge you to investigate why the value is double encoded though, instead of just passing it through several times. Quote Link to comment https://forums.phpfreaks.com/topic/256053-special-characters-from-php-variable-within-javascript/#findComment-1312695 Share on other sites More sharing options...
unifiedac Posted January 30, 2012 Author Share Posted January 30, 2012 Passing it through a second time didn't work. Neither did applying the json_encode() function outside the Javascript. The string comes from the get_the_category() and get_the_title() Wordpress functions. I'm not sure where the encoding occurs, but it's somewhere in the Wordpress core files, which I would rather not change. Thanks for all your help. Quote Link to comment https://forums.phpfreaks.com/topic/256053-special-characters-from-php-variable-within-javascript/#findComment-1312703 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.