将html实体转换为UTF-8,但保留现有的UTF-8

问题描述 投票:2回答:3

我想将html实体转换为UTF-8,但mb_convert_encoding已经破坏了UTF-8编码的字符。什么是正确的方法?

$text = "äöü ä ö ü ß";
var_dump(mb_convert_encoding($text, 'UTF-8', 'HTML-ENTITIES'));
// string(24) "äöü ä ö ü ß"
php utf-8 html-entities mb-convert-encoding
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) "äöü ä ö ü ß"

Demo


0
投票

在我的localhost中,我得到了string(18) "äöü ä ö ü ß"

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


0
投票

如果仍然没有工作,试试这个

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

这是Windows IIS系统上需要开始正常工作的内容。 see source

© www.soinside.com 2019 - 2024. All rights reserved.