Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simple html dom not working

I am trying to use simple html dom to parse my html data. But whenever i use the below code i get error as :-

Trying to get property of non-object

My code is:-

foreach($html->find('div.row') as $val)
        {
            $name = $val->find('h1')->innertext;
            echo $name;
        }

But when i use it like this i get the proper result:-

foreach($html->find('div.row') as $val)
        {
            foreach($val->find('h1') as $v)
             echo $v->innertext;
        }
like image 276
Manoj K Avatar asked Nov 26 '25 07:11

Manoj K


1 Answers

The find() method returns an array unless you specify an index as the second argument. Your second piece of code works as it loops through the array and gets the innertext of each element in the array, whereas your first piece of code is trying to get the innertext property of the actual array.

If you want to get the first (or only) occurrence of h1 for example, do this:

$name = $val->find('h1', 0)->innertext;
like image 103
Matt Cain Avatar answered Nov 27 '25 21:11

Matt Cain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!