我想将 html 实体转换为 UTF-8,但是 mb_convert_encoding
破坏了已经 UTF-8 编码的字符。正确的做法是什么?
$text = "äöü ä ö ü ß";
var_dump(mb_convert_encoding($text, 'UTF-8', 'HTML-ENTITIES'));
// string(24) "äöü ä ö ü ß"
最佳答案
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) "äöü ä ö ü ß"
关于php - 将 html 实体转换为 UTF-8,但保留现有的 UTF-8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31338277/