In the JSON does not support Chinese, use it to send Chinese data will appear data loss or garbled, must be sent before the transmission of the string to encode, because the transmission in the past need to use JS data analysis, considering the JS has unescape function, so if there is an escape function in PHP, Encode the data and decode it with unescape on the client, which is much more convenient.
First search on the internet, a lot of PHP to implement the escape function, the same as the following:
function Phpescape ($STR) {
Preg_match_all ("/[x80-xff].| [x01-x7f]+/", $str, $r);
$ar = $r [0];
foreach ($ar as $k => $v) {
if (Ord ($v [0]) < 128)
$ar [$k] = Rawurlencode ($v);
Else
$ar [$k] = "%u". Bin2Hex (Iconv ("GB2312", "UCS-2", $v));
}
return join ("", $ar);
}
This function works very well, but perhaps some novice does not understand the principle of the function (such as me), it is always uneasy to use, and now I will explain the principle of this function. And I think it's like standing on the shoulders of a giant with someone else's code, but if you don't understand someone else's code, you'll fall to the ground sooner or later.
The first sentence: Preg_match_all ("/[x80-xff].| [x01-x7f]+/, $str, $r); This is a regular expression that matches all the characters in the string, [X80-xff]. Matching is the Chinese character, X represents the matching character of the 16-encoded, [] is a class selector, "." represents any character, so [X80-xff]. The match is two characters, the first of which is the 16 character from 80 to FF, which happens to be the first character of the encoding. This will be a complete match of a Chinese character. On the code of Chinese characters in Unicode, we can search the Internet. Similarly, [x01-x7f]+ English string, because the earliest English is ASCII encoding, the encoding value is less than 128, that is, 16 binary from 01 to 7f, "+" represents one or more characters, so [x01-x7f]+ can match consecutive multiple English strings.
$ar = $r [0]; $r [0] where the storage is matched to the array
foreach ($ar as $k => $v) {
if (Ord ($v [0]) < 128)//If the character encoding value is less than 128, the description is an English character
$ar [$k] = Rawurlencode ($v); Direct coding with Rawurlencode
Else
$ar [$k] = "%u". Bin2Hex (Iconv ("GB2312", "UCS-2", $v)); Otherwise, use the Iconv function to convert Chinese characters into ucs-2 encoding, or Unicode encoding.
}
This is about PHP in the escape function of an implementation, welcome to add
</