Home  >  Article  >  Backend Development  >  php jsonp single quote escape, jsonp quote escape_PHP tutorial

php jsonp single quote escape, jsonp quote escape_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:13:26911browse

php jsonp single quote escape, jsonp quote escape

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

callbackname('json string');

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> . '\');';

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>);

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...
Statement:
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