簡體   English   中英

將HTML字符串附加到DOMDocument節點

[英]Append HTML string to DOMDocument node

我試圖創建一個新的,精簡的DOM但是我找不到一種方法來獲取從DOMDocument::saveHTML()返回的HTML字符串並將其插入到新的DOMDocument

我想取$courseHTML並將其附加到<body>節點。

這是可能的,還是我的方法不是最優的?

// grab the original DOM
$doc = new DOMDocument();
$doc->loadHTML($content, LIBXML_HTML_NOIMPLIED);
$body = $doc->getElementsByTagName('body');

// create a new DOM just for the <body> content
$mock = new DOMDocument();
foreach ($body->item(0)->childNodes as $child) {
   $mock->appendChild($mock->importNode($child, true));
}

$courseHTML = $mock->saveHTML();

// create a new, stripped-down DOM
$doc = new DOMDocument();
$html = $doc->appendChild($doc->createElement('html'));
$head = $html->appendChild($doc->createElement('head'));

$node = $head->appendChild($doc->createElement('meta'));
$node->setAttribute('charset', 'utf-8');

$node = $head->appendChild($doc->createElement('meta'));
$node->setAttribute('name', 'viewport');
$node->setAttribute('content', 'width=device-width, initial-scale=1');

$style = $head->appendChild($doc->createElement('style'));
$style->setAttribute('type', 'text/css');

$rangersCSS = $doc->createTextNode('@import url("https://crmpicco.co.uk/grfc1872.css");');
$style->appendChild($rangersCSS);

$body = $html->appendChild($doc->createElement('body'));
$body->setAttribute('id', 'crmpicco_course');

$doc->formatOutput = true;

$content = '<!DOCTYPE html>';
$content .= $doc->saveHTML();

此外,除了字符串連接之外,是否有更簡潔的方法將DOCTYPE添加到DOM?

可以通過從HTML創建DOMDocumentFragment來插入任意一段HTML字符串。 請注意,這僅適用於HTML格式良好的XML。

可以通過使用DOMImplementation DOMDocument對象來創建具有doctype聲明的文檔。

例如:

<?php
$myhtml = '<p>Here is some <abbr title="hypertext markup language">HTML</abbr></p>';

$doc = (new DOMImplementation)->createDocument(
    null,
    "html",
    (new DOMImplementation)->createDocumentType("html")
);

// html element is already created
$html = $doc->getElementsByTagName('html')[0];
$head = $html->appendChild($doc->createElement('head'));

$node = $head->appendChild($doc->createElement('meta'));
$node->setAttribute('charset', 'utf-8');

$node = $head->appendChild($doc->createElement('meta'));
$node->setAttribute('name', 'viewport');
$node->setAttribute('content', 'width=device-width, initial-scale=1');

$style = $head->appendChild($doc->createElement('style'));
$style->setAttribute('type', 'text/css');

$rangersCSS = $doc->createTextNode('@import url("https://crmpicco.co.uk/grfc1872.css");');
$style->appendChild($rangersCSS);

$body = $html->appendChild($doc->createElement('body'));
$body->setAttribute('id', 'crmpicco_course');

// can't just use new DOMDocumentFragment for some reason
$fragment = $doc->createDocumentFragment();
$fragment->appendXML($myhtml);
$body->appendChild($fragment);

$doc->formatOutput = true;

echo $doc->saveHTML();

輸出:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style type="text/css">@import url("https://crmpicco.co.uk/grfc1872.css");</style>
</head>
<body id="crmpicco_course"><p>Here is some <abbr title="hypertext markup language">HTML</abbr></p></body>
</html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM