Home > php教程 > php手册 > php jsonp单引号转义,jsonp引号转义

php jsonp单引号转义,jsonp引号转义

WBOY
Release: 2016-06-13 09:20:30
Original
1039 people have browsed it

php jsonp单引号转义,jsonp引号转义

php中jsonp输出时一般用下面的格式:

callbackname('json string');
Copy after login

如果中间的json string中含有单引号,这个输出就是有问题的,调用方一般是无法处理的,所以我们要对单引号进行转义。

如果是用json_encode方式生成可以用下面的方式转义:

<span>$ret</span> = json_encode(<span>$result</span>,<span> JSON_HEX_APOS);
</span><span>header</span>('Content-Type: text/javascript; charset=utf-8'<span>);
</span><span>echo</span> <span>$callback</span> . '(\'' . <span>$ret</span> . '\');';
Copy after login

这里 JSON_HEX_APOS 是php是提供的把单引号换为 \u0027 了。

如果是字符串拼接的,可以用下面的方式:

<span>$jsonData</span> = <span>preg_replace</span>('/\'/', '\u0027', <span>$jsonData</span>);
Copy after login

然后再输出。

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template