0

This is a simple code: (simple html dom)

$html = file_get_html('http://google.com');

$title = $html->find('div.newsBody h1', 0)->plaintext;
echo "Title:". $title;

If div.newsBody dosen't exist in google page we get this error:

Notice: Trying to get property of non-object in...

Now I want to handle this error:

 if(file_get_html('http://google.com'))
 { 
     $html = file_get_html('http://google.com');
     if($html->find('div.newsBody h1', 0))
         echo $html->find('div.newsBody h1', 0)->plaintext;
     else
         echo "No div found!";
 }
 else
     echo "No webpage Access!";

Is it true? or IS there a better solution?

ali raha
  • 211
  • 1
  • 2
  • 14

1 Answers1

0

That's how it's done, but don't repeat yourself:

if($div = $html->find('div.newsBody h1', 0))
  echo $div->plaintext;
pguardiario
  • 53,827
  • 19
  • 119
  • 159