2016-05-12 181 views
1

我有以下字符串:如何将Unicode特殊字符转换为html实体?

$string = "★ This is some text ★"; 

我想把它转换为HTML实体:

$string = "★ This is some text ★"; 

的解决方案大家都在写:

htmlentities("★ This is some text ★", "UTF-8"); 

但ヶ辆不能将所有的unicodes转换为html实体。所以它只是给我的输出与输入相同的:

★ This is some text ★ 

我也试过这种解决方案既结合:

header('Content-Type: text/plain; charset=utf-8'); 

和:

mb_convert_encoding(); 

但是,这两种打印和空结果,根本不转换或错误地将星星转换为:

 

如何将★和所有其他unicode字符转换为正确的html实体?

+0

的http:// php.net/manual/en/function.htmlentities.php#107985 – iainn

回答

4

htmlentities不会在这种情况下工作,但你可以尝试UCS-4编码字符串,喜欢的东西:

$string = "★ This is some text ★"; 
$entity = preg_replace_callback('/[\x{80}-\x{10FFFF}]/u', function ($m) { 
    $char = current($m); 
    $utf = iconv('UTF-8', 'UCS-4', $char); 
    return sprintf("&#x%s;", ltrim(strtoupper(bin2hex($utf)), "0")); 
}, $string); 
echo $entity; 

★ This is some text ★ 

Ideone Demo

相关问题