In earlier versions of PHP, escape special characters in Json and escape json_PHP

Source: Internet
Author: User
In earlier versions, PHP escapes special characters in Json and escapes json. In the old version of PHP, special characters in Json are escaped, json is used as an API for an App, data is retrieved from MySQL on the server side, and JSON is generated. There is a field in the data called the function of escaping special characters in Json in PHP of the old conte version, and escaping json

Create an API for an App, extract data from MySQL on the server, and generate JSON. There is a field in the data called content, which stores the content of the article and contains a large number of HTML tags. This field needs to be escaped when converted to json, because a large number of special characters will damage the json structure.

For example, the following content:

The code is as follows:
'Lorem ipsum "dolor" sit amet, consectetur \ adipiscing elit .'

It must be converted:

The code is as follows:
Lorem ipsum \ "dolor \" sit amet, \ nconsectetur \ adipiscing elit.

If the PHP version is later than 5.2, json_encode comes with an escape character. If it is an old version of PHP, you can use the following function.

# list from www.json.org: (\b backspace, \f formfeed)public function escapeJsonString($value) {  $escapers = array("\\", "/", "\"", "\n", "\r", "\t", "\x08", "\x0c");  $replacements = array("\\\\", "\\/", "\\\"", "\\n", "\\r", "\\t", "\\f", "\\b");  $result = str_replace($escapers, $replacements, $value);  return $result;}

It is often used. record it and hope to help you.

The producer performs an API for an App, extracts data from MySQL on the server, and generates JSON. There is a field in the data called conte...

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.