2

I have following html file

<body>
<div class="container">
    <div class="list">
        <ul>
            <li>
                <a href="http://website1.com" ><img src="image1.jpg"></a>
            </li>
            <li>
                <a href="http://website2.com" ><img src="image2.jpg"></a>
            </li>
            <li>
                <a href="http://website3.com" ><img src="image3.jpg"></a>
            </li>
            ....
            ....
            ....
        </ul>
    </div>
</div>
</body>

By parsing the above html file. I want output like this..

 http://website1.com
 image1.jpg
 http://website2.com
 image2.jpg
 http://website3.com
 image3.jpg
 ....
 ....

By seeing above output, you can guess that I only need 'href' and 'src' value of each list item.

I am trying to parse by using 'simple_html_dom' 3rd party plugin.

<?php
  include_once('simple_html_dom.php');
  $html = new simple_html_dom();
  $html->load_file("html_file.html");
  foreach($html->find('div[class=list] ul li') as $li)
   {
     echo $li->find('a')->href."<br />";
     echo $li->find('img')->src."<br />";
   }
 ?>

but the above code doesn't work. Please tell me if i did anything wrong or else use can assist me by using PHP DOM module if you know.

1
  • This is not PHPs "DOM module", but an 3rd party library. Commented Dec 29, 2014 at 10:14

2 Answers 2

1

Instead of finding div[class=list] ul li's, why don't you search for .list li's? Documentation for Simple HTML DOM is quite lacking, so you'll need to pay around with it yourself, but this should work:

foreach($html->find('.list ul li') as $li)

If that still doesn't work, I'd print_r($html->find('.list ul il'), to see what that produces, and if that's an empty array, just cut back on the selectors until you can find the source of the problem (ie print_r($html->find('.li ul'), etc)

Sign up to request clarification or add additional context in comments.

2 Comments

Oh, if my answer helped solved the problem, could you accept it? :)
both print_r($html->find('.list ul il')) and print_r($html->find('div[class=list] ul li') gives same output. it gives list of array elements..
0

<?php include_once('simple_html_dom.php'); $html = new simple_html_dom(); $html->load_file("html_file.html"); foreach($html->find('div.list a') as $a) { echo $a->href."<br />"; echo $a->children(0)->src."<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.