1

我要在这个问题上大发雷霆了。如果有人有任何解决方案。我有一个 html 字符串

$html = '<div id="main">What is going on </div><div>&#1740;&#1729;&#1575;&#1722; 
&#1578;&#1608; &#1705;&#1608;&#1574;&#1740; &#1729</div>
<span>Some More Text &lt;good&gt;</span>;

这是具有 html 实体 + 英文字符 + unicode 字符的数字符号的混合 html 字符串。我只想将 unicode 字符的数字符号转换为实际的 unicode 字符值。还有一些我不想丢失的用户格式。

我想要以下输出

$html = '<div id="main">What is going on </div><div>‘۔سلطان محمود نے گاڑی روکتے ہوئے</div>
<span>Some More Text &lt;good&gt;</span>;

我用过

html_entity_decode($html, ENT_COMPAT, 'utf-8');

但这也将转换为我&lt;不想要的。<&gt;>

还有其他解决方案吗??

注意:我并不是要求我的网页上没有正确显示 unicode 字符,它们显示得很好。因为网页呈现数字符号并显示为真正的 unicode 字符。但我也想要网页后面的实际 unicode 字符。

4

1 回答 1

1

尝试使用 preg_preplace_callback 和 html_entity_decode 作为回调。

$decode_single_entity = function ($matches) {
    return html_entity_decode($matches[0], ENT_COMPAT, 'utf-8');
};
$string = preg_replace_callback('/&#\d+;/', $decode_single_entity, $html);
于 2013-04-05T13:13:49.833 回答