Search code examples
javascriptphpapostrophe

PHP need to output ' instead of '


I have an issue with jbar where it's outputting a ' and causing the javascript to break. My code is:

<div class="jbar" data-init="jbar" data-jbar='{
"message" : "<?php echo $myann->getValue("heading", 0); ?>",
"button"  : "<?php echo $myann->getValue("buttonText", 0); ?>",
"url"     : "<?php echo $myann->getValue("buttonURL", 0); ?>",
"state"   : "open"
}'></div>

If I manually type in:

"message" : "My name's test",

This breaks still. If I type in:

"message" : "My name&#39;s test",

This works!

So... How can I get the php echo to use the ' instead of the '? I've tried htmlspecialchars and addslashes but neither work.

Thanks.


Solution

  • From the documentation:

    "'" (single quote) becomes ''' (or ') only when ENT_QUOTES is set.

    So

    echo htmlspecialchars($input, ENT_QUOTES);