2

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 ?

1

4 Answers 4

10

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 ;
       }
     }
   }
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

thanks . But I didnt choose this library , my project manager did !
1

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')

Comments

0

here is a method to select elements that fits all those criteria, at once. It returns an array of the elements that fit in all the criteria, or an array of zero elements. It accepts either an array of elements or a single element. The criteria needs to be delimited by commas, and it will returned elements that fit in all those criteria. Example usage

$dom = new simple_html_dom();
$dom->load($Email);
//This would select elements with only those selected criteria
//Then what is returned is an array, and then we select further elements that
//are em
$selection = GetSelectionMultiple($dom, "font, .ff2, .fs16, .fc1, .fwn, .gf");
$em = GetSelectionMultiple($selection, "em");


function GetSelectionMultiple($single_element_or_array, $criteria, $delimiter=",")
{
$array_split = explode($delimiter, $criteria);
$remove_emptyones = [];
for($index=0; $index<count($array_split); $index++)
{
    $sel = trim($array_split[$index]);
    if(strlen($sel) > 0)
    {
        $remove_emptyones[] = $sel;
    }
}
$array_split = $remove_emptyones;



//Elements which will be returned
$array_element = [];
$dom_array = [];
if(is_array($single_element_or_array) == false)
{
    $dom_array[] = $single_element_or_array;
}
else
{
    foreach($single_element_or_array as $iterate)
    {
        $dom_array[] = $iterate;
    }
}


for( $domindex=0; $domindex<count($dom_array); $domindex++ )
{
    $current_find = null;
    $dom = $dom_array[$domindex];

    for($index=0; $index<count($array_split); $index++)
    {
        $use = trim($array_split[$index]);
        if(strlen($use) == 0)
            continue;

        if($index == 0)
        {
            $current_find = $dom->find($use);
            foreach($current_find as $element)
            {
                $array_element[] = $element;
            }
        }
        else
        {
            //Each array element find things now and add them or remove them
            for($check=0; $check<count($array_element); $check++ )
            {
                $parent = $array_element[$check]->parent;
                if($parent == null || $parent == false)
                {
                    continue;
                }

                $current_find = $parent->find($use);
                if($current_find != null && $current_find && count($current_find) > 0)
                {
                    foreach($current_find as $element)
                    {
                        $matches = false;
                        for($c=0; $c<count($array_element); $c++)
                        {
                            if($array_element[$c] === $element)
                            {
                                $matches =true;
                                break;
                            }
                        }
                        if($matches == false)
                        {
                            $array_element[$check] = null;
                        }
                    }
                }
                else
                {
                     $array_element[$check] = null;
                }
            }

            //Remove the null elements
            $newarray = [];
            for($check=0; $check<count($array_element); $check++ )
            {
                $current_find = $array_element[$check];
                if($current_find != null)
                {
                    $newarray[] = $current_find;
                }
            }
            $array_element = $newarray;
        }
    }
 }

    return $array_element;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.