Home > php教程 > php手册 > PHP字符编码问题之GB2312 VS UTF-8

PHP字符编码问题之GB2312 VS UTF-8

WBOY
Release: 2016-06-13 10:42:30
Original
940 people have browsed it

今天照着书随便写了段代码,代码意图是将字符串使用str_split()函数进行分割成数组,英文好说,但分割中文(两个中文一个数组单元)时就出问题了——

  看代码:

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

 

 

New Document  

  

 

  

  

   

 $string1 = "i am a phper";      

  $string2 = "这本书是蓝海战略";       

 print_r(str_split($string1));       

echo "
";       

print_r(str_split($string2,4));  

  ?>

  

测试结果打出我所料——中文乱码

  Why?Why?Why?Why?乱码是什么?什么事乱码?给我解释解释,什么,是%&的乱码!

  因为英文无乱码,只有中文乱码,首先想到了编码的问题,于是突然想起来UTF-8的编码是UTF-8需要3个字节,死马当活马医吧!

  于是  print_r(str_split($string2,4));这句中的4 ,就被换成了6,于是乎——看结果

  同样,你也可以试试将编码的charset的UTF-8改成GB2312,因为Unicode的编码是需要2字节的,所以说Gb2312的编码比UTF-8能够节约1/3的空间,但是如果你要兼容繁体中文、韩文、日文的其他的语言就需要使用UTF-8了。

  顺便发现了首动感的Music,拿来分享一下:

  —Its Ok?

  —Its OK!

 

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template