PHP universal functions for retrieving Chinese character unicode codes

Source: Internet
Author: User
Conversion of a Chinese character into a unicode universal function does not rely on any library, and other user-defined functions, but the condition is that the input parameters of this file and function should be UTF-8 encoded, otherwise, function conversion is required. Conversion of a Chinese character into a unicode universal function does not rely on any library, and other user-defined functions, but the condition is that the input parameters of this file and function should be UTF-8 encoded, otherwise, function conversion is required.

/**
* Conversion of a Chinese character into a unicode universal function does not rely on any library, and other user-defined functions, but conditional
* Condition: the input parameters of this file and function should be UTF-8 encoded. otherwise, function conversion is required.
* In fact, reverse conversion functions can also be easily compiled, not limited to Chinese characters. it is strange why php does not have ready-made functions.
* @ Author xieye
*
* @ Param {string} $ word must be a Chinese character or an array representing Chinese characters (cut using str_split)
* @ Return {string} a decimal unicode code, such as 4f60, indicates the Chinese character "you"
*/
Function getUnicodeFromOneUTF8 ($ word ){
// Obtain the internal array representation of its characters. Therefore, UTF-8 encoding is used in this file!
If (is_array ($ word ))
$ Arr = $ word;
Else
$ Arr = str_split ($ word );
// At this time, $ arr should be similar to array (228,189,160)
// Define an empty string for storage
$ Bin_str = '';
// Convert the string to a number, convert it to a binary string, and then combine them.
Foreach ($ arr as $ value)
$ Bin_str. = decbin (ord ($ value ));
// At this time, $ bin_str should be similar to 111001001011110110100000, if it is a Chinese character "you"
// Regular truncation
$ Bin_str = preg_replace ('/^. {4 }(. {4 }). {2 }(. {6 }). {2 }(. {6}) $/',' $1 $2 $ 3', $ bin_str );
// At this time, $ bin_str should be similar to 0100111101100000, if it is a Chinese character "you"
Return bindec ($ bin_str); // return a 20320-type Chinese character "you"
// Return dechex (bindec ($ bin_str); // If you want to return the hexadecimal 4f60, use this sentence
}
?>

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.