PHP 解析问题 -  和 Â

2022-01-07 00:00:00 parsing character-encoding php html

当我尝试解析一些带有   的 html 然后 echo 它时, 变成"这个字符: Â.此外,html_entity_decode()str_replace() 不会改变它.

When I try to parse some html that has   sprinkled through it and then echo it, the   "turns into" this character: Â. Also, html_entity_decode() and str_replace() doesn't change it.

为什么会这样?如何删除 Â 的?

Why is this happening? How can I remove the Â's?

推荐答案

不间断空格存在于两个字节的UTF-8中:<代码>0xC2 和 0xA0.

The non-breaking space exist in UTF-8 of two bytes: 0xC2 and 0xA0.

当这些字节在 ISO-8859-1 中表示时(单字节编码)而不是 UTF-8(多字节编码),那么这些字节分别成为字符 Â 和另一个不间断空格 .

When those bytes are represented in ISO-8859-1 (a single-byte encoding) instead of UTF-8 (a multi-byte encoding) then those bytes becomes respectively the characters  and another non-breaking space .

显然您正在使用 UTF-8 解析 HTML 并使用 ISO-8859-1 回显结果.要解决此问题,您需要要么使用 ISO-8859-1 解析 HTML,或 使用 UTF-8 回显结果.我建议一直使用 UTF-8.浏览 PHP UTF-8 备忘单 以将其全部对齐.

Apparently you're parsing the HTML using UTF-8 and echoing the results using ISO-8859-1. To fix this problem, you need to either parse HTML using ISO-8859-1 or echo the results using UTF-8. I'd recommend to use UTF-8 all the way. Go through the PHP UTF-8 cheatsheet to align it all out.

相关文章