pg.
pg.

Reputation: 2531

Using DOMDocument & loadHTML in PHP to find out the class of a tag

I want to get all LI tags that are inside OL tags which have the "bananas" class, so for example if the HTML looks like:

<ol class="pluots plums grapes">
    <li>back to the future 2</li>
    <li>lemon bars</li>
</ol>

<ol class="walnuts bananas cornbread">
    <li>mustard</li>
    <li>ketchup</li>
</ol>

I want to be able to get mustard & ketchup only.

So this here:

@$doc->loadHTML($html);
$ols = $doc->getElementsByTagName('ol');
  foreach ($ols as $ol) {
     echo "<br/>". $ol->nodeName. ": ";
     $nodes = $ol->childNodes;
     foreach ($nodes as $node) {
          echo $node->nodeValue. "\n";
    }
}

will get me all of the LI's but I want to get only the ones from the OL that has "bananas" class?

I can get the class names kind of clumsily by doing this:

foreach ($ol->attributes as $attr) {
    $value = $attr->nodeValue;
}

I just want to be able to call the class for an OL so I figured it would be something like:

$class=$ol->attributes->[????]->nodeValue to get the class but I can't seem to figure it out.

I need to be able to select by class because sometimes these OLs will be in different order.

Upvotes: 2

Views: 4375

Answers (1)

Robby Cornelissen
Robby Cornelissen

Reputation: 97237

While looping through them, check the value of the class attribute to see if it contains bananas.

<?
$html = <<<HTML
<ol class="pluots plums grapes">
    <li>back to the future 2</li>
    <li>lemon bars</li>
</ol>

<ol class="walnuts bananas cornbread">
    <li>mustard</li>
    <li>ketchup</li>
</ol>
HTML;

$doc = new DOMDocument();
$doc->loadHTML($html);
$ols = $doc->getElementsByTagName('ol');

foreach ($ols as $ol) {
    $classes = $ol->getAttribute('class');

    if (strpos($classes, 'bananas') !== false) {
        $nodes = $ol->getElementsByTagName('li');

        foreach ($nodes as $node) {
              echo trim($node->nodeValue)."\n";
        }   
    }   
}

This prints out:

mustard
ketchup

As for your second question, the MDN HTML Reference as well as the MDN DOM Reference are both really complete.

Upvotes: 1

Related Questions