2016-05-12 175 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

相關問題