Home > Backend Development > PHP Tutorial > Analysis on the Principle of Chinese Character Base Conversion in PHP_PHP Tutorial

Analysis on the Principle of Chinese Character Base Conversion in PHP_PHP Tutorial

WBOY
Release: 2016-07-21 15:22:28
Original
1115 people have browsed it

1. Analysis of the principle of converting Chinese characters to decimal

A Chinese character in GBK encoding consists of two characters. The method of obtaining a Chinese character string is as follows

Copy the code The code is as follows:

$string = "Don't be obsessed with me";
$length = strlen($string);
for($i=0 ;$i<$length;$i++){
if(ord($string[$i])>127){
$result[] = ord($string[$i]).' ' .ord($string[++$i]);
}
}
var_dump($result);

php-ord-dec-number
Since one Chinese character is two It consists of 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 two characters of a Chinese character through the for loop method, and then use the ord() function to convert each character to decimal. The above are respectively: Don’t [178 187] Want [210 170] Fan [195 212] Love [193 181] Brother [184 231]

2. Analysis of the principle of converting Chinese characters to hexadecimal

Use the UltraEdit development tool to directly view the hexadecimal of Chinese characters, as shown below

For example, view the hexadecimal of the five words "Don't be obsessed with brother"
ultraedit-16-hex
From the above picture, you can know that the hexadecimal characters corresponding to each Chinese character are: Not B2BB, D2AA, Fan C3D4, Love C1B5, Brother B8E7

PHP realizes the conversion of Chinese characters to hexadecimal The principle of system is to first use the ord() function to extract the decimal number of each Chinese character. For details, please refer to [PHP Functions Chapter Mastering the Application of ord() and chr() Functions], and then use the dechex() function to convert each Chinese character into hexadecimal System

Example source code
Copy code The code is as follows:

$string = "Don't be obsessed with brother ";
$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);
//Hex
$strings = array();
foreach( $result as $v){
$dec = explode(" ",$v);
$strings[] = dechex($dec[0])." ".dechex($dec[1]) ;
}
var_dump($strings);

The result is as shown below

php-dec-hex-string

Use the above method to convert Chinese characters into hexadecimal. The output result can be compared with the hexadecimal obtained using the UltraEdit development tool.

3. Analysis of the principle of converting Chinese characters to binary and octal

The conversion of Chinese characters to binary and octal is the same as the above hexadecimal conversion principle, just the conversion function Different, combined with the above example code, the following

conversion of Chinese characters to binary is implemented as follows:
Copy the code The code is as follows:

$strings = array();
foreach($result as $v){
$dec = explode(" ",$v);
$strings[] = decbin($ dec[0])." ".decbin($dec[1]);
}
var_dump($strings);

The result is as follows:
php-dec-bin-string
Convert Chinese characters to octal, the method is as follows
Copy the code The code is as follows:

$strings = array();
foreach($result as $v){
$dec = explode(" ",$v);
$strings[] = decoct($dec[0])." ".decoct($dec[ 1]);
}

The results are as follows:
php-string-hex
Understand the principle of Chinese character hexadecimal conversion in PHP, and then use the PHP built-in function urldecode() to convert the hexadecimal string into normal Chinese through combination For Chinese characters, please pay attention to the character encoding principles of the urldecode() and urlencode() functions in the next issue of the Chinese character encoding research series.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324690.htmlTechArticle1. Analysis of the principle of converting Chinese characters to decimal. In GBK encoding, a Chinese character consists of two characters. Obtain the Chinese character string The method is as follows. Copy the code as follows: $string = "Don't be obsessed with me...
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