1

I trying to iterate over this following html using simple html dom parser but not getting to next node.

<div class="category-products">
<ul class="products-grid">
    <li><a href="http://someurl.com">Text</a></li>
    <li><a href="http://someurl.com">Text</a></li>
    <li><a href="http://someurl.com">Text</a></li>
</ul>
<ul class="products-grid">
    <li><a href="http://someurl.com">Text</a></li>
    <li><a href="http://someurl.com">Text</a></li>
    <li><a href="http://someurl.com">Text</a></li>
</ul>
<ul class="products-grid">
    <li><a href="http://someurl.com">Text</a></li>
    <li><a href="http://someurl.com">Text</a></li>
    <li><a href="http://someurl.com">Text</a></li>
</ul>
</div>

I want to iterate over each li and a for ul.products-grid but i am not getting to next ul tag I have used this code yet.

require_once('simple_html_dom.php');
set_time_limit(0);
ini_set('memory_limit', '1024M');
$url='http://www.somesite.com';
$html = file_get_html($url);

if(is_object($html)){
    foreach ( $html->find('div.category-products') as $elem){
        $data = $elem->innertext;
        $strdata = str_get_html($data);
        foreach ($strdata->find('ul[class="products-grid"]') as $ul) {
                //not getting how to iterate over next ul tags
        }
        die;
    }
}

Any body having any idea please help to sort it out. Thanks

2
  • i am using simplehtmldom.sourceforge.net simple html dom parser Commented Apr 22, 2013 at 17:30
  • Is it possible to provide the correct url , so that we could help you because we can use example you provided , as object Commented Apr 22, 2013 at 18:03

2 Answers 2

4

You're overcomplicating it:

foreach ( $html->find('div.category-products li a') as $a){
  // do something with $a
}
Sign up to request clarification or add additional context in comments.

Comments

0

You may try this

$url='http://www.somesite.com';
$html = file_get_html($url);
if(is_object($html)){
    foreach ( $html->find('div.category-products .products-grid') as $ul){
        // To iterate al li tags use following
        foreach ( $ul->find('li') as $li )  echo $li. '<br />';
        // To iterate al a tags use following
        foreach ( $ul->find('li a') as $a ) echo $a. '<br />';
    }
}

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.