php jsonp single quote escape

WBOY
Release: 2016-08-08 09:33:13
Original
915 people have browsed it

Both PHP single quotes and double quotes can modify string type data, if the modified string contains variables (such as $name); the biggest difference is: double quotes will replace the value of the variable, while single quotes will treat it as a character string output.

The following format is generally used when outputting jsonp 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 to 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.

The above introduces php jsonp single quote escaping, including the content of jsonp single quote escaping. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!