0

I am trying to get some contents from one html file and load that content into another html file using php. I have loaded the first html file as DOMdocument. Now I would like to get the contents inside a certain tag of that loaded html file. I have searched for the solution in the internet but I could only find the methods to get the value of content inside the tag. I would like to get all the contents inside the given tag. i.e including the inner html tags. How can i do it?

I have a html file like this:

<html>
<head>
</head>
<body>
</body>
<p>
<h1> hi </h1>
</p>
</html>

A domdocument is created using this html file. Now what i would like to do is get the contents:

    <p>
    <h1> hi </h1>
    </p>

as a string.

In short, I am in need of a function something like :

 $doc->getElementsByTagName('p').item(0).innerHTML;

Here $doc is a DomDocument.

$doc = new DomDocument;
$doc->Load('test.html');
Suraz
  • 3
  • 2
  • possible duplicate of [How do you parse and process HTML/XML in PHP?](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) – Noy Aug 17 '14 at 09:29

2 Answers2

0

This comment in the manual might help you:

http://php.net/manual/en/book.dom.php#89718

<?php
function DOMinnerHTML($element)
{
    $innerHTML = "";
    $children = $element->childNodes;
    foreach ($children as $child)
    {
        $tmp_dom = new DOMDocument();
        $tmp_dom->appendChild($tmp_dom->importNode($child, true));
        $innerHTML.=trim($tmp_dom->saveHTML());
    }
    return $innerHTML;
}
?>

Example:

<?php
$dom= new DOMDocument();
$dom->load($html_string);
$dom->preserveWhiteSpace = false;

$domTable = $dom->getElementsByTagName("table");

foreach ($domTable as $tables)
{
    echo DOMinnerHTML($tables);
}
?>

UPDATE:

btw.: a h* tag is invalid inside a p tag. In firebug for example you see that firefox automatically closes the p tag before the h tag and opens it again after it.

jvecsei
  • 1,936
  • 2
  • 17
  • 24
0

The most straightforward way to get the inner HTML of a DOM node (if you don't mind modifying the original document) is to create a new DOMDocumentFragment and move the child nodes into it:

<?php

$html = '<body><p><span> hi </span></p></body>';

$doc = new DOMDocument();
$doc->loadHTML($html);

$p = $doc->getElementsByTagName('p')->item(0);

$fragment = $doc->createDocumentFragment();

while ($p->firstChild) {
  $fragment->appendChild($p->firstChild);
}

print $doc->saveHTML($fragment); // <span> hi </span>
Alf Eaton
  • 5,226
  • 4
  • 45
  • 50