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

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -