Search code examples
phpsimple-html-dom

php dom parser return parent and child


I think this is a simple question but I can't sort it, I am trying to get all heading tags with the simple php DOM parser, my code works only one way, example

 $heading['h2']=$html->find('h2 a');//works fine

I have found some sites wrap the h2 within the a tag like this

<a href='#'><h2> my heading</h2></a>

The problem is trying to get both tags so I can display the link with it. So when I do this

$heading['h2']=$html->find('a h2');

I get the h2 fine but it will not wrap the link tag around it, which of course makes sense, find all h2 tags that are children of a but how do I get the entire parent tag, I hope that makes sense, what I want it to return is

<a href="#"><h2>My Headings</h2></a>

then I can just print the output with

echo $headings['h2']; //and the link with be there

Solution

  • If the <a href="[..]"> ist just the outer element, you can do it like this:

    $heading['h2']=$html->find('a h2');
    foreach ($heading['h2'] as $h2) {
        echo $h2->parent(), "\n";
    }
    

    You could also go up the DOM tree until you reach an <a> tag:

    $heading['h2']=$html->find('a h2');
    foreach ($heading['h2'] as $h2) {
        $a = $h2;
        while ($a && $a->tag != "h2") $a = $a->parent();
        if (!$a) continue; // no <a> above <h2>
        echo $a, "\n";
    }