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

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

    Use DOMDocument to keep what you need rather than strip what you don't need (PHP >= 5.3.6)

    $d = new DOMDocument;
    $d->loadHTMLFile($fileLocation);
    $body = $d->getElementsByTagName('body')->item(0);
    // perform innerhtml on $body by enumerating child nodes 
    // and saving them individually
    foreach ($body->childNodes as $childNode) {
      echo $d->saveHTML($childNode);
    }
    

提交回复
热议问题