Home > Backend Development > PHP Tutorial > Three methods for converting Chinese characters and hexadecimal encoding in PHP

Three methods for converting Chinese characters and hexadecimal encoding in PHP

WBOY
Release: 2016-07-25 08:53:51
Original
1930 people have browsed it
  1. //Convert Chinese characters to hexadecimal encoding
  2. function hexencode($s) {
  3. return preg_replace('/(.)/es',"str_pad(dechex(ord('\1 ')),2,'0',str_pad_left)",$s);
  4. }
  5. //Convert hexadecimal encoding to Chinese characters
  6. function hexdecode($s) {
  7. return preg_replace('/(w{2}) /e',"chr(hexdec('\1'))",$s);
  8. }
  9. echo hexdecode(hexencode("Welcome to Beijing!"));
  10. ?>
Copy code

Method 2,

  1. echo rawurlencode("Welcome to Beijing").'
    ';
Copy code

Returns a string, all non-letters in this string except -_. Numeric characters will be replaced with a percent sign (%) followed by two hexadecimal digits. To decode: rawurldecode

Method 3, gbk version: In gbk encoding, a Chinese character consists of two characters. If the ascii value of the character obtained through the ord() function is greater than 127, it can be determined that the current character is the first half of a Chinese character, and the second half of the Chinese character needs to be obtained. Of course, this method of judgment must be combined with the specific development environment. If there is a single character with an ascii value greater than 127, this method of judgment is obviously incorrect.

The principle of converting Chinese characters to decimal in PHP is to obtain the two characters of a Chinese character through the for loop method, and then use the ord() function to convert each character into decimal. The above are respectively: No [178 187] Want [210 170] Fan [195 212] Love [193 181] Brother [184 231] PHP implements the principle of converting Chinese characters to hexadecimal: First, use the ord() function to get the decimal number of each Chinese character. For details, please see [php function article to master the application of ord() and chr() functions], and then use the dechex() function to convert each Chinese character into hexadecimal. Code:

  1. $string = "Welcome to Beijing!";
  2. $length = strlen($string);
  3. echo $string;
  4. $result = array();
  5. //Decimal
  6. for ($i=0;$i<$length;$i++){
  7. if(ord($string[$i])>127){
  8. $result[] = ord($string[$i]).' '.ord($string[++$i]);
  9. }
  10. }
  11. var_dump($result);
  12. echo '
    ';
  13. //Hex
  14. $strings = array();
  15. foreach($result as $v){
  16. $dec = explode(" ",$v);
  17. $strings[] = dechex($dec[0])." ".dechex($dec[1]);
  18. }
  19. var_dump($strings);
Copy code

utf-8 version:

  1. $string = "Welcome to Beijing!";
  2. $length = strlen($string);
  3. echo $string;
  4. $result = array();
  5. //Decimal
  6. for($i=0 ;$i<$length;$i++){
  7. if(ord($string[$i])>127){
  8. $result[] = ord($string[$i]).' '.ord($ string[++$i]).' '.ord($string[++$i]);
  9. }
  10. }
  11. var_dump($result);
  12. echo '
    ';
  13. //hex Make
  14. $strings = array();
  15. foreach($result as $v){
  16. $dec = explode(" ",$v);
  17. $strings[] = dechex($dec[0])." ". dechex($dec[1])." ".dechex($dec[2]);
  18. }
  19. var_dump($strings);
Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template