Mohamed Athif Mohamed Athif - 4 months ago 53
PHP Question

simple html dom find gives error Trying to get property of non-object

i am using simple_html_dom to get a text from a website but it gives me

Notice: Trying to get property of non-object in C:\wamp64\www\fetch.php on line 6

the website has this.

<div class="info">number: (IUL)306/306/2016/68</div>
<div class="info">published date: 14 june 2016</div>
<div class="info">published time: 1442</div>
<div class="info">expiry time 23 june 2016 1100</div>

my php looks like this which i am trying to get the second div with class info.

$html = file_get_html('');

foreach($html->find('div[class=info]', 2) as $e)
$pdate = $e->innertext . '<br>';

echo "pdate: $pdate";

any possible solutions to achieve this?


If you're providing the index:

$html->find('div[class=info]', 2)
                               ^ this one

You're going to get the element directly. So you might as well use the ->innertext straight ahead. You already got the third element:

$info = $html->find('div[class=info]', 2);
echo $info->innertext;

No need for that foreach.

If you need that foreach, then just use this alone:

                      // ^ note: the second argument is omitted

This will give you the collection of elements found. Example:

foreach($html->find('div[class=info]') as $e) {
    echo $e->innertext, '<br/>';

This is actually covered in the manual.

Its under How to find HTML elements? section.