Make an API for an APP, fetch data from MySQL on the server side, and generate JSON. There is a field in the data called content, which holds the contents of the article, contains a large number of HTML tags, this field in the transfer of JSON need to escape, because a large number of special characters will destroy the JSON structure.
such as the content of this paragraph:
Copy Code code as follows:
' Lorem ipsum ' dolor ' sit amet, consectetur \ adipiscing elit. '
It must be converted to:
Copy Code code as follows:
Lorem ipsum \ "dolor\" sit amet,\nconsectetur \ adipiscing elit.
If PHP version > 5.2,json_encode is escaped with itself. If it's 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 used, recorded, hope to help you.