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

后端 未结 7 1588
情话喂你
情话喂你 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:40

    Since the substr() method seemed to be too much for some to swallow, here is a DOM parser method:

    $d = new DOMDocument;
    $mock = new DOMDocument;
    $d->loadHTML(file_get_contents('/path/to/my.html'));
    $body = $d->getElementsByTagName('body')->item(0);
    foreach ($body->childNodes as $child){
        $mock->appendChild($mock->importNode($child, true));
    }
    
    echo $mock->saveHTML();
    

    http://codepad.org/MQVQ3XQP

    Anybody wish to see that "other one", see the revisions.

提交回复
热议问题