Escaping/encoding single quotes in JSON encoded HTML5 data attributes

JSONWeb DevelopmentFront End ScriptsJavascript

To escape single quotes, use json_encode() to echo arrays in HTML5 data attributes.

printf('<article data-tags="%s">', htmlspecialchars(json_encode(array('html5', ...)), ENT_QUOTES, 'UTF-8'));

Or you can also using built-in

json_encode(array('html5', ...), JSON_HEX_APOS)
raja
Published on 05-Apr-2018 14:09:05
Advertisements