2

I want to convert html entities to UTF-8, but mb_convert_encoding destroys already UTF-8 encoded characters. Whats the correct way?

$text = "äöü ä ö ü ß";
var_dump(mb_convert_encoding($text, 'UTF-8', 'HTML-ENTITIES'));
// string(24) "äöü ä ö ü ß"
4

3 回答 3

4

mb_convert_encoding()不是您想要实现的正确功能:您应该真正使用html_entity_decode()代替,因为它只会将实际的 html 实体转换为 UTF-8,并且不会影响现有的 UTF-8 字符在字符串中。

$text = "äöü ä ö ü ß";
var_dump(html_entity_decode($text, ENT_COMPAT | ENT_HTML401, 'UTF-8'));

这使

string(18) "äöü ä ö ü ß"

演示

于 2015-07-10T10:39:27.287 回答
0

在我的本地主机中,我得到string(18) "äöü ä ö ü ß".

我认为这与您的页面编码有关。使用 Notepad++ 编辑文件,然后从工具栏中转到编码并更改为“在 ANSI 中编码”。如果它不起作用,请尝试使用“在没有 BOM 的情况下以 UTF-8 编码”。

于 2015-07-10T10:30:42.650 回答
0

如果这仍然不起作用,试试这个

html_entity_decode($html, ENT_QUOTES, 'cp1252');

这是在 Windows IIS 系统上开始正常工作所需要的。见源

于 2018-03-15T05:32:52.827 回答