PHP将JSON字符串回显为HTML输入值-需要字符转义 [英] PHP Echoing JSON string into HTML Input value - Need character escape

查看:115
本文介绍了PHP将JSON字符串回显为HTML输入值-需要字符转义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我遇到问题的代码的简化版本。

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

输入框仅包含{\
我需要一种方法来转义整个JSON字符串

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

感谢
Alex

Thanks Alex

推荐答案

您正在输出到HTML上下文中,因此需要特定于html的转义:

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

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

这篇关于PHP将JSON字符串回显为HTML输入值-需要字符转义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆