@FrankFarmer가 제안한대로 DomDocument::loadHtml
을 사용하고 구문 분석 된 노드를 가져 와서 현재 문서로 가져 오는 것이 해결책입니다. 내 구현은 그냥 또한, 당신은`바로`있는 DOMDocument :: loadHtml()를 사용하여 문서를로드하는 ... 그것을`libxml_use_internal_errors()`를 켜고 무시해야 할 수도 있습니다이
/**
* 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;
}
처럼 보인다? –
@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