A function of PHP escaping special characters in JSON
Make an API for an App, pull the data from the server-side MySQL, and then generate the JSON. There is a field in the data called content, which holds the content of the article, contains a lot of HTML tags, this field should be escaped when the JSON is transferred, because there are a large number of special characters will destroy the structure of the JSON.
Such a content:
' Lorem ipsum ' dolor ' sit amet, consectetur \ adipiscing elit. '
It must be converted to:
Lorem ipsum \ "dolor\" sit amet,\nconsectetur \ adipiscing elit.
So what are the characters that need to be escaped? See:
If PHP version > 5.2,json_encode comes with Escape. If you have an older 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;}
Often use, record, hope to help you.
http://www.bkjia.com/PHPjc/951262.html www.bkjia.com true http://www.bkjia.com/PHPjc/951262.html techarticle PHP escapes the special characters in JSON by making an API for an App, extracting the data from the server-side MySQL, and then generating the JSON. There is a field in the data called content, which holds the article ...