1

I'm using the PHP Simple HTML DOM Parser to get e-mail addresses from 10.000+ pages.

require_once('simple_html_dom.php'); 
$html = file_get_html('http://www.myurl');
$email = $html->find('dl', 5)->children(3);

Sometimes get the follow error. Probably because some page's don't have the tag:

Fatal error: Call to a member function children() on a non-object

How to avoid this error if a page doesn't contain the info I'm looking for without interrupting the complete script?

1
  • The find() failed to locate anything, so what it returns has no ->children() method. Commented Jul 5, 2013 at 21:57

2 Answers 2

2

You could test to see if $email is an object by using the is_object() function, e.g.

$email = $html->find('dl', 5);
if(is_object($email) === true)
{
    print_r($email->children(3));
}
else continue;

This would probably be quicker than testing for children using an external library since it uses a function already present in the PHP engine.

Sign up to request clarification or add additional context in comments.

Comments

2

Don't chain your methods if there's uncertainty. Assign $email = $html->find('dl', 5) - then test if there are any children, probably with hasChildNodes

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.