Php Chinese characters and hexadecimal encoding conversion methods

Source: Internet
Author: User
Tags alphanumeric characters
Php Chinese characters and hexadecimal encoding conversion methods

  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 ("Beijing welcomes you! "));
  10. ?>

Method 2,

  1. 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:

  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. // Hexadecimal
  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 );

UTF-8:

  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. // Hexadecimal
  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 );

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.