Finding and Printing all Links within a DIV

Posted by Abs on Stack Overflow See other posts from Stack Overflow or by Abs
Published on 2010-06-01T00:24:58Z Indexed on 2010/06/01 0:33 UTC
Read the original article Hit count: 660

Filed under:
|
|

Hello all,

I am trying to find all links in a div and then printing those links.

I am using the Simple HTML Dom to parse the HTML file. Here is what I have so far, please read the inline comments and let me know where I am going wrong.

include('simple_html_dom.php');  

$html = file_get_html('tester.html');

$articles = array();

//find the div the div with the id abcde
foreach($html->find('#abcde') as $article) {

    //find all a tags that have a href in the div abcde
    foreach($article->find('a[href]') as $link){

        //if the href contains singer then echo this link
        if(strstr($link, 'singer')){

            echo $link;

        }

    }

}

What currently happens is that the above takes a long time to load (never got it to finish). I printed what it was doing in each loop since it was too long to wait and I find that its going through things I don't need it to! This suggests my code is wrong.

The HTML is basically something like this:

<div id="abcde">
<!-- lots of html elements -->
<!-- lots of a tags -->
<a href="singer/tom" />
<img src="image..jpg" />
</a>
</div>

Thanks all for any help

© Stack Overflow or respective owner

Related posts about php

Related posts about dom