1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > php json引号转义字符 PHP JSON字符串 转义JS输出的双引号

php json引号转义字符 PHP JSON字符串 转义JS输出的双引号

时间:2024-01-23 06:48:18

相关推荐

php json引号转义字符 PHP JSON字符串 转义JS输出的双引号

我正在从一个

PHP数组创建一个JSON字符串.我已经使用json_encode()编码了它.

$json_array = array(

'title' => 'Example string\'s with "special" characters'

);

$json_decode = json_encode( $json_array );

在JS文件中,我可以通过以下方式访问信息:

注意:$json_decode使用wp_localize_script()本地化到全局变量PHP_params

var discographyJsonString = PHP_params.playlist_setup.replace( /"/g,'"' ),discographyJson = jQuery.parseJSON( discographyJsonString );

console.log(discographyJson);

输出结果为:

{ "title":"Example string's with "special" characters" }

如果将该结果输入/,您将收到错误.如果您在“特殊”周围取出双引号,则表示有效.

从PHP中创建JSON字符串的最佳方式是什么,并将其转义为正确使用在JS文件中?

301_18@

@H_301_18@

/manual/en/function.json-encode.php#100565

That said,quotes ” will produce invalid JSON,but this is only an issue if you’re using json_encode() and just expect PHP to magically escape your quotes. You need to do the escaping yourself.

@H_301_18@

总结

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。