Home > Backend Development > PHP Tutorial > string - 关于PHP 字符串 编码问题

string - 关于PHP 字符串 编码问题

WBOY
Release: 2016-06-06 20:51:32
Original
1033 people have browsed it

$str='手指固';
echo str_replace('腹','大',$str);
Copy after login
Copy after login

页面为GBK 编码,这样居然能替换。

查了一下各自的编码

指:D6B8
固:B9CC

腹:B8B9
Copy after login
Copy after login

指固:D6B8B9CC
腹:B8B9

这样替换出错,该怎么替换才最好?

回复内容:

$str='手指固';
echo str_replace('腹','大',$str);
Copy after login
Copy after login

页面为GBK 编码,这样居然能替换。

查了一下各自的编码

指:D6B8
固:B9CC

腹:B8B9
Copy after login
Copy after login

指固:D6B8B9CC
腹:B8B9

这样替换出错,该怎么替换才最好?

最直接的函数是 mb_ereg_replace 你把它不当正则用就行。

或者你也可以用iconv/mbstring库里的 mb_substr/iconv_substr 和 mb_strpos/iconv_strpos 来写一个针对多字节编码可以用的 mb_str_replace / iconv_str_replace 。

Related labels:
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