PHP Simple HTML DOM Parser : Select two classes

My problem is very simple . Let's say I have multiple 'a' :

    <td class="autoindex_td">

        <a class="autoindex_a snap_shots" href="http://example.com.html">
            <img height="16" width="16" src="http://exmple.com/download/index_icons/winxp/sound.png" alt="[mp3]"></img>
            <strong>

                05 - example.mp3

            </strong>
        </a>
   </td>

I just want to find 'strong' which has two classes . Here what I tried :

foreach ($html->find('a[class=autoindex_a , class=snap_shots]') as $link) {
    if(isset($link)){
   foreach($link->find('strong') as $tag)
       {
             $name = $tag->plaintext ;
             $hiren[] = $name ;
       }
     }
   }

But I get null .So how do I select two class at the same time ?

Answers


Just found a way :

 foreach ($html->find('a[class=autoindex_a snap_shots]') as $link) {
    if(isset($link)){
   foreach($link->find('strong') as $tag)
       {
             $name = $tag->plaintext ;
             $hiren[] = $name ;
       }
     }
   }

Why not DOMDocument Class ?

<?php

$html=' <td class="autoindex_td">

        <a class="autoindex_a snap_shots" href="http://example.com.html">
            <img height="16" width="16" src="http://exmple.com/download/index_icons/winxp/sound.png" alt="[mp3]"></img>
            <strong>

                05 - example.mp3

            </strong>
        </a>
   </td>';

$dom = new DOMDocument;
@$dom->loadHTML($html);
foreach ($dom->getElementsByTagName('a') as $tag) {
    if ($tag->getAttribute('class') === 'autoindex_a snap_shots') {
        foreach($tag->getElementsByTagName('strong') as $strongTag)
        {
        echo $strongTag->nodeValue; //"prints"  05 - example.mp3
        }
    }
}

You need to separate the attribute finders as:

$html->find("a[class='autoindex_a'][class='snap_shots']")

or, separate the classes with a dot . separator:

$html->find('a.autoindex_a.snap_shots')

Need Your Help

What alternative to HTTP_HOST can I use?

php apache security host

I have to display current server host (in PHP, but it is not very important). I don't know on which host I am.

FInding length of String in NASM

string assembly nasm

I'm trying to check the length of a sting given in the program argument as part of a larger program. I need to place the value of the string length in a variable called n, which I've left uninitial...