将HTML字符串附加到DOMDocument节点

时间:2019-04-04 08:46:18

标签: php html dom domdocument

我正在尝试创建一个新的,精简的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?

1 个答案:

答案 0 :(得分:0)

可以通过从HTML创建DOMDocumentFragment来插入任意HTML字符串。请注意,这仅在HTML也是格式正确的XML时有效。

通过使用DOMDocument构造DOMImplementation对象,可以创建带有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>