首页 > 解决方案 > 将 unicode 字符转换为 html 特殊字符

问题描述

我将 unicode 字符嵌入到这样的字符串中:

64\u00be w × 19\u00bd d × 16\u00be h in (164 × 50 × 43 cm)

现在我想像这样转换它们:

64¾ w × 19½ d × 16¾ h in 164 × 50 × 43 cm

到目前为止,我试过这个:

$desc = '64\u00be w × 19\u00bd d × 16\u00be h in (164 × 50 × 43 cm)';
echo mb_convert_encoding($desc, 'ASCII', 'HTML-ENTITIES')
echo html_entity_decode( mb_convert_encoding ( $desc, 'HTML-ENTITIES', 'ASCII' )

但是两者都不会转换 unicode 字符,我在这里缺少什么?

标签: phpunicode

解决方案


您可以使用这样的简单 hack(它不应该有任何性能问题):

$a = '64\u00be w × 19\u00bd d × 16\u00be h in (164 × 50 × 43 cm)';
$html = json_decode('"' . $a . '"');

echo $html;

推荐阅读