Get contents of BODY without DOCTYPE, HTML, HEAD and BODY tags

后端 未结 7 1779
情话喂你
情话喂你 2021-02-12 17:45

What I am trying to do is include an HTML file within a PHP system (not a problem) but that HTML file also needs to be usable on its own, for various reasons, so I need to know

7条回答
  •  不要未来只要你来
    2021-02-12 18:30

    Use a DOM parser. this is not tested but ought to do what you want

    $domDoc = new DOMDocument();
    $domDoc.loadHTMLFile('/path/to/file');
    $body = $domDoc->GetElementsByTagName('body')->item(0);
    foreach ($body->childNodes as $child){
        echo $child->C14N(); //Note this cannonicalizes the representation of the node, but that's not necessarily a bad thing
    }
    

    If you want to avoid cannonicalization, you can use this version (thanks to @Jared Farrish)

提交回复
热议问题