PHP DOMDocument stripping HTML tags

后端 未结 1 614
时光说笑
时光说笑 2020-12-10 08:22

I\'m working on a small templating engine, and I\'m using DOMDocument to parse the pages. My test page so far looks like this:

         


        
相关标签:
1条回答
  • 2020-12-10 08:45

    Nothing: nodeValue is the concatenation of the value portion of the tree, and will never have tags.

    What I would do to make an HTML fragment of the tree under $node is this:

    
    $doc = new DOMDocument();
    foreach($node->childNodes as $child) {
        $doc->appendChild($doc->importNode($child, true));
    }
    return $doc->saveHTML();
    

    HTML "fragments" are actually more problematic than you'd think at first, because they tend to lack things like doctypes and character sets, which makes it hard to deterministically go back and forth between portions of a DOM tree and HTML fragments.

    0 讨论(0)
提交回复
热议问题