是否有将unicode数字符号转换为实际字符的解决方案?


Is there any solution for unicode numeric symbols conversion to actual characters

这个问题真让我头疼。如果有人有解的话。我有一个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字符

尝试使用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);