【问题标题】:Append HTML string to DOMDocument node将 HTML 字符串附加到 DOMDocument 节点
【发布时间】:2019-08-25 22:50:30
【问题描述】:

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

【问题讨论】:

    标签: php html dom domdocument


    【解决方案1】:

    可以通过从 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>
    

    【讨论】:

      猜你喜欢
      • 2017-11-09
      • 2014-09-16
      • 2020-03-09
      • 2011-11-11
      • 2014-09-09
      • 1970-01-01
      • 2011-07-28
      相关资源
      最近更新 更多