Barata
Barata

Reputation: 2839

PHP - Simple HTML Dom Parser

I'm trying to retrieve some information from a website using PHP Simple HTML Dom Parser. In the website there are many tables with the class "forumborder2" and inside them i want to get some information. In the next example i want the image source.

<table class="forumborder2" width=100% cellspacing=0 cellpadding=0 border=0 align=center>
        <tr>
            <td class="titleoverallheader2" background="modules/Forums/templates/chunkstyle/imagesnew/forumtop.jpg" style="border-top:0px; border-left:0px; border-right:0px;" width="100%" colspan=7 Align=left> 
                <b>Supernatural </b>(2005)&nbsp;&nbsp; - &nbsp;&nbsp;Enviada por: <b>AlJoSi</b>&nbsp;&nbsp; em 6 de Dezembro, 2011 (22:35:11)
            </td>
        </tr>
        <tr height=25>
            <td class="colour12" align=right width=100>
                <b>Idioma:</b> 
            </td>
            <td width=110 class="colour22"> 
                <img src="modules/Requests/images/fPortugal.png" width=18 height=12 border=0 alt='' title=''>
            </td>
        </tr> </table>

I did the following:

foreach($html->find('table[class="forumborder2"]')as $tr){
     echo $tr->children(1)->children(1)->src; }

This always gives the error: "Trying to get property of non-object". If i only go to $tr->children(1)->children(1) i can get <img src="modules/Requests/images/fPortugal.png" width=18 height=12 border=0 alt='' title=''> so why can't i access the src attribute.

Upvotes: 0

Views: 2170

Answers (1)

Robert Van Sant
Robert Van Sant

Reputation: 1507

can't you just grab all the images using the HTML Dom Parser? I'm not sure if if you're only grabbing it from a certain section of the HTML, but if you are, you could run a regex on the image source to get the ones you're looking for; here's a code snippet that might help:

// Create DOM from URL or file
$html = file_get_html('http://www.google.com/');

// Find all images 
foreach($html->find('img') as $element) 
   echo $element->src . '<br>';

// Find all links 
foreach($html->find('a') as $element) 
   echo $element->href . '<br>';

i hope this helps

Upvotes: 4

Related Questions