我想到的解決方案是使用@FrankFarmer建議的DomDocument::loadHtml
,然後將解析的節點導入到我當前的文檔中。我的實現看起來像這樣
/**
* Parses HTML into DOMElements
* @param string $html the raw html to transform
* @param \DOMDocument $doc the document to import the nodes into
* @return array an array of DOMElements on success or an empty array on failure
*/
protected function htmlToDOM($html, $doc) {
$html = '<div id="html-to-dom-input-wrapper">' . $html . '</div>';
$hdoc = DOMDocument::loadHTML($html);
$child_array = array();
try {
$children = $hdoc->getElementById('html-to-dom-input-wrapper')->childNodes;
foreach($children as $child) {
$child = $doc->importNode($child, true);
array_push($child_array, $child);
}
} catch (Exception $ex) {
error_log($ex->getMessage(), 0);
}
return $child_array;
}
你可能只需要打開'libxml_use_internal_errors()'和忽略它......此外,你加載使用'的DomDocument :: loadHtml()'權的文件? –
@FrankFarmer,內部錯誤只是從視覺上或從錯誤處理程序中抑制錯誤,它不會真正解決問題。至於'loadHtml',我不是。我正在使用['DOMDocumentFragment :: appendXML'](http://www.php.net/manual/en/domdocumentfragment.appendxml.php) – wmarbut
請參閱[這個答案](http://stackoverflow.com/questions/ 4645738/domdocument-appendxml-with-special-characters) - HTML不是XML – Owlvark