問題描述
網(wǎng)絡(luò)服務(wù)器以 utf-8 編碼提供響應(yīng),所有文件都以 utf-8 編碼保存,我知道的所有設(shè)置都已設(shè)置為 utf-8 編碼.
The webserver is serving responses with utf-8 encoding, all files are saved with utf-8 encoding, and everything I know of setting has been set to utf-8 encoding.
這是一個快速程序,用于測試輸出是否有效:
Here's a quick program, to test if the output works:
<?php
$html = <<<HTML
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Test!</title>
</head>
<body>
<h1>☆ Hello ☆ World ☆</h1>
</body>
</html>
HTML;
$dom = new DOMDocument("1.0", "utf-8");
$dom->loadHTML($html);
header("Content-Type: text/html; charset=utf-8");
echo($dom->saveHTML());
程序的輸出為:
<!DOCTYPE html>
<html><head><meta charset="utf-8"><title>Test!</title></head><body>
<h1>☆ Hello ☆ World ☆</h1>
</body></html>
呈現(xiàn)為:
我可能做錯了什么?要告訴 DOMDocument 正確處理 utf-8,我需要具體多少?
What could I be doing wrong? How much more specific do I have to be to tell the DOMDocument to handle utf-8 properly?
推薦答案
DOMDocument::loadHTML()
需要一個 HTML 字符串.
DOMDocument::loadHTML()
expects a HTML string.
HTML 使用 ISO-8859-1
編碼(ISO 拉丁字母第 1 號)作為其規(guī)范的默認(rèn)編碼.這是因為更長的時間,請參閱 6.1.HTML 文檔字符集.實際上,這更多是常見網(wǎng)絡(luò)瀏覽器對 Windows-1252
的默認(rèn)支持.
HTML uses the ISO-8859-1
encoding (ISO Latin Alphabet No. 1) as default per it's specs. That is since longer, see 6.1. The HTML Document Character Set. In reality that is more the default support for Windows-1252
in common webbrowsers.
我回過頭來是因為 PHP 的 DOMDocument 基于 libxml 并且?guī)砹?HTMLparser 專為 HTML 4.0 設(shè)計.
I go back that far because PHP's DOMDocument is based on libxml and that brings the HTMLparser which is designed for HTML 4.0.
我認(rèn)為可以安全地假設(shè)您可以加載 ISO-8859-1
編碼的字符串.
I'd say it's safe to assume then that you can load an ISO-8859-1
encoded string.
您的字符串是 UTF-8
編碼的.將所有高于 127/h7F 的字符轉(zhuǎn)換為 HTML 實體 就可以了.如果您不想自己做,那就是 mb_convert_encoding
和 HTML-ENTITIES
目標(biāo)編碼所做的:
Your string is UTF-8
encoded. Turn all characters higher than 127 / h7F into HTML Entities and you're fine. If you don't want to do that your own, that is what mb_convert_encoding
with the HTML-ENTITIES
target encoding does:
- 那些具有命名實體的字符,將獲得命名實體.<代碼>€ ->&歐元;
- 其他人獲得他們的數(shù)字(十進(jìn)制)實體,例如<代碼>☆ ->☆
以下代碼示例通過使用回調(diào)函數(shù)使進(jìn)度更加明顯:
The following is a code example that makes the progress a bit more visible by using a callback function:
$html = preg_replace_callback('/[x{80}-x{10FFFF}]/u', function($match) {
list($utf8) = $match;
$entity = mb_convert_encoding($utf8, 'HTML-ENTITIES', 'UTF-8');
printf("%s -> %s
", $utf8, $entity);
return $entity;
}, $html);
您的字符串的示例輸出:
This exemplary outputs for your string:
☆ -> ☆
☆ -> ☆
☆ -> ☆
無論如何,這只是為了更深入地了解您的字符串.您希望將其轉(zhuǎn)換為 loadHTML
可以處理的編碼.這可以通過將 US-ASCII
之外的所有內(nèi)容轉(zhuǎn)換為 HTML 實體來實現(xiàn):
Anyway, that's just for looking deeper into your string. You want to have it either converted into an encoding loadHTML
can deal with. That can be done by converting all outside of US-ASCII
into HTML Entities:
$us_ascii = mb_convert_encoding($utf_8, 'HTML-ENTITIES', 'UTF-8');
請注意您的輸入實際上是 UTF-8 編碼的.如果您甚至有混合編碼(某些輸入可能會發(fā)生這種情況)mb_convert_encoding
只能處理每個字符串一種編碼.我已經(jīng)在上面概述了如何在正則表達(dá)式的幫助下更具體地進(jìn)行字符串替換,所以我現(xiàn)在留下更多細(xì)節(jié).
Take care that your input is actually UTF-8 encoded. If you have even mixed encodings (that can happen with some inputs) mb_convert_encoding
can only handle one encoding per string. I already outlined above how to more specifically do string replacements with the help of regular expressions, so I leave further details for now.
另一種選擇是提示編碼.這可以通過修改文檔并添加
The other alternative is to hint the encoding. This can be done in your case by modifying the document and adding a
<meta http-equiv="content-type" content="text/html; charset=utf-8">
這是一個指定字符集的內(nèi)容類型.對于無法通過網(wǎng)絡(luò)服務(wù)器使用的 HTML 字符串(例如,保存在磁盤上或在您的示例中的字符串中),這也是最佳實踐.網(wǎng)絡(luò)服務(wù)器通常將其設(shè)置為響應(yīng)標(biāo)頭.
which is a Content-Type specifying a charset. That is also best practice for HTML strings that are not available via a webserver (e.g. saved on disk or inside a string like in your example). The webserver normally set's that as the response header.
如果你不關(guān)心錯位的警告,你可以把它加在字符串前面:
If you don't care the misplaced warnings, you can just add it in front of the string:
$dom = new DomDocument();
$dom->loadHTML('<meta http-equiv="content-type" content="text/html; charset=utf-8">'.$html);
根據(jù) HTML 2.0 規(guī)范,只能出現(xiàn)在文檔的 部分的元素將自動放置在那里.這也是這里發(fā)生的事情.輸出(漂亮的打印):
Per the HTML 2.0 specs, elements that can only appear in the <head>
section of a document, will be automatically placed there. This is what happens here, too. The output (pretty-print):
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta charset="utf-8">
<title>Test!</title>
</head>
<body>
<h1>☆ Hello ☆ World ☆</h1>
</body>
</html>
這篇關(guān)于PHP DOMDocument 無法處理 utf-8 字符 (☆)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!