1 回答

TA貢獻1812條經驗 獲得超5個贊
這很尷尬,因為當您使用它時,它將嘗試修復原始文檔中的HTML。這創建了一個結構,它不是你所認為的。loadHTML()
但是,如果您有文檔的基本大綱,則以下內容會將標記的內容復制到新文檔(代碼中的注釋)...<body>
$html = '<html><body><div>Content1</div></body></html>
<html><body><div>Content2</div></body></html>
<html><body><div>Content3</div></body></html>';
libxml_use_internal_errors(true);
$newDom = new DOMDocument();
// New document with final code
$newBody = new DOMDocument();
$newDom->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
// Set up basic template for new doucument
$newBody->loadHTML("<html><body /></html>");
// Find where to add any new content
$addBody = $newBody->getElementsByTagName("body")[0];
// Find the existing content to add
$bodyTags = $newDom->getElementsByTagName("body");
foreach($bodyTags as $body) {
// Add all of the contents of the <body> tag into the new document
foreach ( $body->childNodes as $node ) {
// Import the node to copy to the new document and add it in
$addBody->appendChild($newBody->importNode($node, true));
}
}
echo $newBody->saveHTML();
這給了...
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><div>Content1</div><div>Content2</div><div>Content3</div></body></html>
限制是不會保留標記之外的任何內容和標記的任何屬性。<body><body>
- 1 回答
- 0 關注
- 142 瀏覽
添加回答
舉報