How to extract html comments and all html contained by node?

前端 未结 4 614
猫巷女王i
猫巷女王i 2020-12-01 22:57

I\'m creating a little web app to help me manage and analyze the content of my websites, and cURL is my favorite new toy. I\'ve figured out how to extract info about all so

4条回答
  •  醉酒成梦
    2020-12-01 23:30

    Comment nodes should be easy to find in XPath with the comment() test, analogous to the text() test:

    $comments = $xpath->query('//comment()'); // or another path, as you prefer
    

    They are standard nodes: here is the manual entry for the DOMComment class.


    To your other question, it's a bit trickier. The simplest way is to use saveXML() with its optional $node argument:

    $html = $dom->saveXML($el);  // $el should be the element you want to get 
                                 // the HTML for
    

提交回复
热议问题