Sarath
Sarath

Reputation: 39

Extract table data from HTML page in php

I have a html table with multiple rows and each row with multiple columns. A Sample for one row looks like this.

<table class ="classt">
    <tbody>
        <tr class="row">
            <td height="20" valign="top" class="mosttext-new">data</td>
            <td height="20" valign="top" class="mosttext-new"> data</td>
            <td height="20" valign="top" class="mosttext-new">data</td> 
        </tr>
    </tbody>
</table>

I am trying to extract all td elements like this in a php script.

foreach($html->find('table.classt') as $e){
         foreach ($e->find('tr.row') as $tr){
            foreach ($tr->find('td') as $td){
                    $text = $td->innertext;
            }
        }
    }

But in $tr I am not getting row details with td tags. It is just coming the entire row withing double quotes like this

"data data data"

so my third loop is not able to find td as $tr does not have td tags.

Any idea on this?

Upvotes: 0

Views: 306

Answers (1)

saravanan
saravanan

Reputation: 51

I think you have to mention the class name after the 'td' followed by '.' like this

 foreach ($tr->find('td.mosttext-new') as $td)

Hope this should solve your problem. All the best.

Upvotes: 5

Related Questions