Character encoding in PHP: gb2312 vs UTF-8

Source: Internet
Author: User

Today, I wrote a paragraph according to the book.CodeThe purpose of the code is to split the string into Arrays Using the str_split () function. It is easy to say in English, but there is a problem when splitting the Chinese character (two Chinese characters and one array unit --

Check the Code:

 <!  Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd"  >  
< Html >
< Head >
< Meta HTTP-equiv = "Content-Type" Content = "Text/html; charsets = UTF-8" />
< Title > New Document </ Title >
< Meta Name = "Author" Content = "" />
< Meta Name = "Keywords" Content = "" />
< Meta Name = "Description" Content = "" />
< Link REL = "Stylesheet" Type = "Text/CSS" Href = "" />
</ Head >

< Body >
<? PHP
$ String1 = "I Am a phper ";
$ String2 = "This book is a blue ocean strategy ";
Print_r (str_split ($ string1 ));
Echo "<br/> ";
Print_r (str_split ($ string2, 4 ));
?>
</ Body >
</ Html >

Test result output as expected --Chinese garbled characters

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

Because English without garbled characters, only Chinese garbled characters, first of all think of the encoding problem, so suddenly think of the UTF-8 encoding is UTF-8 needs 3 bytes, dead horse as a doctor!

Therefore, print_r (str_split ($ string2, 4); 4 in this sentence is replaced6, So -- view the result

Similarly, you can also try to change the UTF-8 of charset encoding to gb2312, because unicode encoding requires 2 bytes, so the gb2312 encoding can save 1/3 of the space than the UTF-8, but if you want to be compatible with other languages of Traditional Chinese, Korean, and Japanese, you need to use UTF-8.

By the way, I found the first dynamic music and shared it with me:

-It's OK?

-It's OK!

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.