[英]json_encode escape special characters
I would like to pass a php
array to a jQuery
function. 我想将
php
数组传递给jQuery
函数。
I tried to send the array as json_encode
like this: 我试图像这样发送数组为
json_encode
:
<button onclick='callFunction(<? echo json_encode($myArray); ?>)'></button>
the result of my json_encode array is: 我的json_encode数组的结果是:
{"Date":"2018-01-26 12:55:00","Details":"FORLI IT"}
All works good. 一切正常。
But if I have an array like this (with a '
) 但是如果我有一个像这样的数组(带有
'
)
{"Date":"2018-01-26 12:55:00","Details":"FORLI' IT"}
My function doesn't work anymore - my console will show: 我的功能不再起作用-我的控制台将显示:
SyntaxError: Unexpected EOF
语法错误:意外的EOF
How can I solve this problem? 我怎么解决这个问题?
尝试使用和addslashes ,
<button onclick='callFunction(<? echo json_encode(addslashes($myArray)); ?>)'></button>
The way I suggest you deal with anything that goes to html is: 我建议您处理所有与html有关的方法是:
<button onclick='callFunction(<? echo htmlentities(json_encode($myArray),ENT_QUOTES); ?>)'></button>
Check all available flags at http://php.net/manual/en/function.htmlentities.php 在http://php.net/manual/en/function.htmlentities.php上检查所有可用的标志
This one uses the flag ENT_QUOTES
because the default behaviour is to only encode double quotes. 此代码使用
ENT_QUOTES
标志,因为默认行为是仅编码双引号。 Using ENT_QUOTES
will also encode single quotes. 使用
ENT_QUOTES
还将对单引号进行编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.