Escape functions in PHP

Source: Internet
Author: User

JSON does not support Chinese characters. If you use JSON to transmit Chinese data, data loss or garbled characters may occur. The strings to be sent must be encoded before transmission, in the past, JavaScript was used for data parsing. Considering that JavaScript contains the Unescape function, If PHP has an escape function that encodes the data and decodes it on the client, this will be much more convenient.

First, search for one on the Internet. Many escape functions implemented using PHP are similar. For example:

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 can work very well, but some new users may not understand the principle of this function (such as me) and are always uneasy to use it. Now I will explain the principle of this function. And I thinkCodeIt's like standing on the shoulders of giants, but if you don't understand other people's code, it will sooner or later fall to the ground.

First sentence: preg_match_all ("/[\ X80-\ xFF]. | [\ x01-\ x7f] +/", $ STR, $ R); this is to use a regular expression to match all characters in the string, [\ X80-\ xFF]. matching Chinese characters, \ X indicates the hexadecimal encoding of matching characters, and [] indicates the class selector, ". "represents any character, as shown in [\ X80-\ xFF]. the match is two characters, the first of which is the hexadecimal character from 80 to ff, and this is exactly the first character of Chinese character encoding. In this way, a Chinese character can be completely matched. You can search for Chinese character encoding in Unicode on the Internet. Similarly, [\ x01-\ x7f] + English string, because the earliest English is ASCII encoding, the encoding value is less than 128, that is, the hexadecimal value from 01 to 7f, "+" indicates one or more characters, so that [\ x01-\ x7f] + can match multiple consecutive English strings.

$ AR = $ R [0]; // $ R [0] Stores matched arrays.
Foreach ($ Ar as $ k => $ v ){
If (ord ($ V [0]) <128) // if the character encoding value is less than 128, it is an English character.
$ Ar [$ K] = rawurlencode ($ V); // use rawurlencode for encoding.
Else
$ Ar [$ K] = "% u ". bin2hex (iconv ("gb2312", "UCS-2", $ V); // otherwise, use the iconv function to convert Chinese characters into ucs-2 encoding, that is, Unicode encoding.
}

This is an implementation of the escape Function in PHP.

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.