PHP Jsonp single quote Escape _php instance

Source: Internet
Author: User

The JSONP output in PHP is generally in the following format:

Copy Code code as follows:

Callbackname (' JSON string ');

If the middle JSON string contains single quotes, the output is problematic and the caller is generally not able to handle it, so we have to escape the single quotation marks.

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

Copy Code code as follows:

$ret = Json_encode ($result, Json_hex_apos);
Header (' Content-type:text/javascript; Charset=utf-8 ');
Echo $callback. ' (\ '. $ret. '\');';

Here Json_hex_apos is provided by PHP to swap single quotes for \u0027.

If the string is spliced, you can use the following method:

Copy Code code as follows:

$jsonData = preg_replace ('/\ '/', ' \u0027 ', $jsonData);

and then output.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.