Frage

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
War es hilfreich?

Lösung

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";
}

Andere Tipps

Well my first thought we be to use

$html->find('a');

But I'm guessing you have multiple links on your page. So the correct practice would then be to use an ID (or a class) to identify your link

<a href='#' id="titleLink"><h2> my heading</h2></a>

And then search for that specific ID:

$html->find('a#titleLink');

I don't know what library you're using and what syntax it supports, but I hope you get the idea anyway.

According to docs: $heading['h2']=$html->find('a > h2')->parent(); would return the anchor tag wrapping the h2, but if you have multiple 'a > h2' in the page, the find function will return an array, so try it and/or use foreach.

$info = $html->find('a,h2');

echo '<a href='.$info[0]->href.'>'.$info[1]->innertext.'</a>';
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top