- // Convert Chinese characters to hexadecimal encoding
- Function hexencode ($ s ){
- Return preg_replace ('/(.) /es ', "str_pad (dechex (ord (' \ 1'), 2, '0', str_pad_left)", $ s );
- }
- // Convert hexadecimal encoding to Chinese characters
- Function hexdecode ($ s ){
- Return preg_replace ('/(\ w {2})/E', "chr (hexdec (' \ 1')", $ s );
- }
- Echo hexdecode (hexencode ("Beijing welcomes you! "));
- ?>
Method 2,
- Echo rawurlencode ("Beijing Welcomes You ").'
';
-
Returns a string. all non-alphanumeric characters except-_. in this string will be replaced with a semicolon (%) followed by two hexadecimal numbers. For decoding: rawurldecode Method 3, gbk: a Chinese character in gbk encoding is composed of two characters. if the ascii value of a character is greater than 127, you can determine that the current character is the first half of a Chinese character and the second half of the Chinese character must be obtained. Of course, this method should be combined with the specific development environment. if there is a single character with an ascii value greater than 127, this method is obviously incorrect. The principle of converting Chinese characters to decimal in php is to use the for loop method to obtain two characters of a Chinese character, and then use the ord () function to convert each character to decimal. The above are: do not [178 187] to [210 170 195] fans [212 193] Love [181 184] brother [231] php to implement the principle of converting Chinese characters to hexadecimal: first, use the ord () function to retrieve the decimal digits of each Chinese character. for details, refer to [php function], and then use dechex () the function converts Chinese characters to hexadecimal. Code:
- $ String = "Welcome to Beijing! ";
- $ Length = strlen ($ string );
- Echo $ string;
- $ Result = array ();
- // Decimal
- For ($ I = 0; $ I <$ length; $ I ++ ){
- If (ord ($ string [$ I])> 127 ){
- $ Result [] = ord ($ string [$ I]). ''. ord ($ string [++ $ I]);
- }
- }
- Var_dump ($ result );
- Echo'
';
- // Hexadecimal
- $ Strings = array ();
- Foreach ($ result as $ v ){
- $ Dec = explode ("", $ v );
- $ Strings [] = dechex ($ dec [0]). "". dechex ($ dec [1]);
- }
- Var_dump ($ strings );
-
UTF-8:
- $ String = "Welcome to Beijing! ";
- $ Length = strlen ($ string );
- Echo $ string;
- $ Result = array ();
- // Decimal
- For ($ I = 0; $ I <$ length; $ I ++ ){
- If (ord ($ string [$ I])> 127 ){
- $ Result [] = ord ($ string [$ I]). ''. ord ($ string [++ $ I]). ''. ord ($ string [++ $ I]);
- }
- }
- Var_dump ($ result );
- Echo'
';
- // Hexadecimal
- $ Strings = array ();
- Foreach ($ result as $ v ){
- $ Dec = explode ("", $ v );
- $ Strings [] = dechex ($ dec [0]). "". dechex ($ dec [1]). "". dechex ($ dec [2]);
- }
- Var_dump ($ strings );
|