How do I convert a string with single quotes into double for json parse
A better approach would be to use the htmlentities()
function to encode the "
as "
, meaning you can insert it as data-*
. When you retrieve it using JavaScript, they'll show up as "
, meaning you can JSON.parse
it immediately;
<a data-foo="<?php echo htmlentities(json_encode(array('demo' => 'test'))); ?>">Hey</a><script>alert(JSON.parse(document.getElementsByTagName("a")[0].dataset.foo).demo);</script>