Consider the code below to send a json string to js from php,
<?php
$str = "<!--<script>"; // This is from user input
?>
<script>
var json_str = <?= json_encode($str) ?>;
</script>
The string will break the HTML, and the way to solve it is via something like the old school comment hack, e.g.
<script>
<!--
var json_str = <?= json_encode($str) ?>;
//-->
</script>
Are there any alternative?
var str = <?= json_encode(strip_tags($str)); ?>;not an option?strip_tags, or even a simplestr_replace(array('<!--', '-->), '', $str)` should do