Home  >  Article  >  Backend Development  >  分割GBK中文遭遇乱码的解决方法_php技巧

分割GBK中文遭遇乱码的解决方法_php技巧

WBOY
WBOYOriginal
2016-05-17 08:56:161171browse

类似如下的字符串(GBK), explode不能得到正确结果:

1.$result = explode("|", "滕华弢|海青"); 究其原因, 对于”弢”字(读tao,不认识没关系,我也不认识), 因为他的GBK编码值为: 8f7c, 不巧的是, “|”的ASCII值也是7c.

这样的问题, 还有很多 : 因为GBK编码的编码范围是: 0×8140-0xfefe, 所以, 理论上来说, 任何低字节是7c的字都会有这个问题, 比如:

1.倈(827c), 億(837c), 眧(b17c), 鍇(e57c).......等等等等对于这样的情况,

1.第一, 可以采用转码到utf8, 然后explode, 再转回来, 这是比较麻烦的方法.
2.第二, 我们可以采用正则拿"匹配出"来代替"分离出":
3.preg_match_all("/([/x81-/xfe][/x40-/xfe])+/", $gbk_str, $matches);//写死编码这样, $matches中0号索引对应的数组就是结果词的数组了..

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