2013-03-29 86 views
3

我使用的CKEditor用於讓用戶發表評論,用戶也可以把Unicode字符在評論欄中。PHP的DOMDocument不渲染Unicode字符正確

當我提交表單,並檢查$ _ POST [「回覆」]中的Unicode字符顯示得非常好。我也在頁面頂部使用header('Content-type:text/html; charset=utf-8'); 但是當我使用PHP DOMDocument處理它時,所有字符都變得不可讀。

$html_unicode = "xyz unicode data"; 
$html_data = '<body>'.$html_unicode . '</body>'; 
$dom = new DOMDocument(); 
$dom->loadHTML($html_data); 

$elements = $dom->getElementsByTagName('body'); 

當我回聲

echo $dom->textContent; 

輸出變爲

​​

我怎樣才能得到正確的Unicode字符後面使用PHP的DOMDocument。

回答

1

感謝上帝給我的解決方案僅通過更換

$html_data = '<body>'.$html_unicode . '</body>'; 

$html_data = '<head><meta http-equiv="Content-Type" 
content="text/html; charset=utf-8"> 
</head><body>' . $html_unicode . '</body>'; 
+0

這個爲我工作。 我只是這樣做的: $ content = str_replace('','',$ content); – Mahmood

3

試試這個:)

<?php 
    $html_unicode = "xyz unicode data"; 
    $html_data = '<body>'.$html_unicode . '</body>'; 
    $dom = new DOMDocument(); 
    $dom->loadHTML($html_data); 

    $elements = $dom->getElementsByTagName('body'); 
    echo utf8_decode($dom->textContent); 
?> 
+0

這僅適用於在ISO-8859-1字符。 OP使用阿拉伯語。 – Esailija

+0

它也適用於阿拉伯語。試一試 –

+0

*將帶有UTF-8編碼的ISO-8859-1字符的字符串轉換爲單字節ISO-8859-1 *。 ISO-8859-1沒有阿拉伯字符。 – Esailija

0

這個工作了阿拉伯語的langauge

<?php 
echo "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=Windows-1256\"></head><body>"; 
$html = file_get_contents(" url "); 
$dom = new DOMDocument(); 
@$dom->loadHTML($html); 
$ExTEXT = $dom->getElementById('tag id'); 
echo utf8_decode($ExTEXT->textContent); 
echo "</body></html>"; 
6

這爲我工作:

$html_unicode = "xyz unicode data"; 
$html_data = '<body>'.$html_unicode . '</body>'; 

$dom = new DOMDocument(); 
$html_data = mb_convert_encoding($html_data , 'HTML-ENTITIES', 'UTF-8'); // require mb_string 
$dom->loadHTML($html_data); 

$elements = $dom->getElementsByTagName('body');