我用这个函数来完成我的HTML。
function correct_html($string) {
$dom = new \DOMDocument;
$dom->loadHTML($string);
$mock = new \DOMDocument;
$body = $dom->getElementsByTagName('body')->item(0);
foreach ($body->childNodes as $child) {
$mock->appendChild($mock->importNode($child, true));
}
return trim($mock->saveHTML());
}
但是当我用é或任何其他特殊字符使用此功能时,它会给我é。我怎样才能解决这个问题
尝试添加
utf8_decode()
在将字符串解析为loadHTML()之前,像这样,
function correct_html($string) {
$dom = new \DOMDocument();
$dom->loadHTML(utf8_decode($string));
$mock = new \DOMDocument;
$body = $dom->getElementsByTagName('body')->item(0);
foreach ($body->childNodes as $child) {
$mock->appendChild($mock->importNode($child, true));
}
return trim($mock->saveHTML());
}
我认为你应该在DOMDocument的构造中添加编码。
$dom = new DOMDocument('1.0', 'UTF-8');
还要确保在标题中设置编码。
header("Content-Type: text/html; charset=UTF-8");