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
}
?> |