Escaping/encoding single quotes in JSON encoded HTML5 data attributes

后端 未结 2 401
栀梦
栀梦 2020-12-01 07:54

In PHP, I use json_encode() to echo arrays in HTML5 data attributes. As JSON requires - and json_encode() generates - values encapsulated by double

相关标签:
2条回答
  • 2020-12-01 08:09

    You need to HTML escape data echoed into HTML:

    printf('<article data-tags="%s">',
        htmlspecialchars(json_encode(array('html5', ...)), ENT_QUOTES, 'UTF-8'));
    
    0 讨论(0)
  • 2020-12-01 08:21

    or use the build-in option:

    json_encode(array('html5', ...), JSON_HEX_APOS)
    

    you can check it up in the manual: http://php.net/manual/en/json.constants.php#constant.json-hex-apos

    0 讨论(0)
提交回复
热议问题