Home  >  Article  >  Backend Development  >  PHP character encoding problem: GB2312 VS UTF-8_PHP tutorial

PHP character encoding problem: GB2312 VS UTF-8_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:44:021037browse

I wrote a piece of code based on the book today. The purpose of the code is to use the str_split() function to split a string into an array. It is easy to speak in English, but there is a problem when splitting Chinese (two Chinese and one array unit)——

Look at the code:

>http://www.w3.org/TR/html4/loose.dtd ">

New Document

$string1 = "i am a phper";

$string2 = "This book is about blue ocean strategy";

print_r(str_split($string1));

echo "
"; 

print_r(str_split($string2,4));

?>

 

The test result came out as I expected - Chinese garbled characters

PHP character encoding problem: GB2312 VS UTF-8_PHP tutorial

Why? Why? Why? Why? What is garbled code? What is garbled code? Explain to me, what is the garbled code of %&!

Because there are no garbled characters in English but only Chinese garbled characters, I first thought about the encoding problem, and then suddenly remembered that the encoding of UTF-8 is that UTF-8 requires 3 bytes. Let’s treat it like a dead horse!

So print_r(str_split($string2,4)); the 4 in this sentence was replaced by 6, so - look at the result

PHP character encoding problem: GB2312 VS UTF-8_PHP tutorial

Similarly, you can also try changing the UTF-8 of the encoded charset to GB2312. Because Unicode encoding requires 2 bytes, Gb2312 encoding can save 1/3 of the space than UTF-8. But if you want to be compatible with other languages ​​such as Traditional Chinese, Korean, and Japanese, you need to use UTF-8.

By the way, I discovered the first dynamic music, and I want to share it with you:

 —Its Ok?

 —Its OK!

http://www.bkjia.com/PHPjc/478805.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478805.htmlTechArticleI wrote a piece of code based on the book today. The purpose of the code is to use the str_split() function to split the string into Arrays are easy to speak in English, but when splitting Chinese (two Chinese characters and one array unit)...
Statement:
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