PHP Echoing JSON string into HTML Input value - Need character escape

烂漫一生 提交于 2020-01-30 05:21:24

问题


Here is a simplified version of my code that I am having a problem with.

$variable = "{\\\"JSON" //long JSON string created in Javascript with JSON.stringify
?> <input type="text" name="somename" value="<?php echo $variable; ?>"/> <?php

The input box only contains {\ I need a way to escape the entire JSON string

Thanks Alex


回答1:


You're outputting into an HTML context, so you need html-specific escaping:

<input ... value="<?php echo htmlspecialchars(json_encode($whatever)); ?>" />
                             ^^^^^^^^^^^^^^^^----


来源:https://stackoverflow.com/questions/21887228/php-echoing-json-string-into-html-input-value-need-character-escape

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!