Home  >  Article  >  Backend Development  >  php jsonp single quote escape

php jsonp single quote escape

WBOY
WBOYOriginal
2016-08-08 09:33:13913browse

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

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

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.

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