javascript - Proper way to escape json data in PHP without using JS comment hack -
consider code below send json string js php,
<?php $str = "<!--<script>"; // user input ?> <script> var json_str = <?= json_encode($str) ?>; </script>
the string break html, , way solve via old school comment hack, e.g.
<script> <!-- var json_str = <?= json_encode($str) ?>; //--> </script>
are there alternative?
you can use flag json_hex_tag
, <
, >
encoded \u003c
, \u003e
respectively.
json_encode($str, json_hex_tag)
Comments
Post a Comment