Home  >  Article  >  Backend Development  >  php如何去除字符串中由于字节丢失造成的乱码字符

php如何去除字符串中由于字节丢失造成的乱码字符

WBOY
WBOYOriginal
2016-06-06 20:38:042206browse

比如字符串 "怎么搭配�1212�宿在游戏中" 出现了乱码字符,猜测是字节丢失造成的。这样的字符串构建的xml拿去解析就会XML parse error: not well-formed (invalid token),要如何写段程序过滤掉字符串中的这些的字符?

回复内容:

比如字符串 "怎么搭配�1212�宿在游戏中" 出现了乱码字符,猜测是字节丢失造成的。这样的字符串构建的xml拿去解析就会XML parse error: not well-formed (invalid token),要如何写段程序过滤掉字符串中的这些的字符?

不是字节丢失,是编码不一致

出错之后前后字符还能正常,猜测是 UTF-8。那样的话,把不符合 UTF-8 编码的字节删去就行了。具体看 UTF-8 的编码试试。

我不懂 PHP。在 Python 里的话,直接指定编码/解码时出错时如何处理就行了,比如 errors='ignore' 就可以忽略掉乱七八糟的部分。

http://php.net/manual/en/function.iconv.php

If you append the string //IGNORE, characters that cannot be represented in the target charset are silently discarded.

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