php jsonp single quote escape, jsonp quote escape_PHP tutorial

WBOY
Release: 2016-07-13 10:13:26
Original
955 people have browsed it

php jsonp single quote escape, jsonp quote escape

The following format is generally used when jsonp output in php:

callbackname('json string');
Copy after login

If the middle json string contains single quotes, there is a problem with this output, and the caller generally cannot handle it, so we need to escape the single quotes.

If it is generated using json_encode, it can be escaped in the following way:

<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

Here JSON_HEX_APOS is provided by php and replace the single quotes with u0027.

If it is string concatenation, you can use the following method:

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

Then output.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/916030.htmlTechArticlephp jsonp single quote escape, jsonp quote escape When jsonp output in php, the following format is generally used: callbackname( 'json string'); If the middle json string contains single quotes, the output will be...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template