-1

So I am just trying to scrape an HTML page with PHP. I looked on Google for how to do it, and I uuse the file_get_contents() method. I wrote a little bit of code, but I am already getting an error that I cannot figure out:

    $page = file_get_contents( 'http://php.net/supported-versions.php' );
    $doc = new DOMDocument( $page );
    //print_r( $page );

foreach ( $doc->getElementsByTagName( 'table' ) as $node ) {
    print_r( $node );
}

The first, commented out print_r statement DOES print the page, but the foreach loop should be getting every table in $node but it is printing nothing. What am I doing wrong?

5
  • 2
    My advice would be to use Simple HTML Dom Parser; simplehtmldom.sourceforge.net Commented Jan 23, 2017 at 19:56
  • why you trying to mix php with javascript Commented Jan 23, 2017 at 19:57
  • I'm writing a WordPress plugin Commented Jan 23, 2017 at 20:03
  • @Kaylined What is wrong with the DOMDocument parser? It is able to do this, he just made a little mistake. Commented Jan 23, 2017 at 20:52
  • Nothing wrong with it; the simplehtmldom just provides a nice wrapper imo. Commented Jan 23, 2017 at 23:59

1 Answer 1

2

You load your DOMDocument wrong, you need to either ->loadHTMLFile() or something a like. See the documentation here.

Here is what you need to do instead.

<?php
    libxml_use_internal_errors(true);
    $doc = new DOMDocument();
    $doc->loadHTMLFile("http://php.net/supported-versions.php");
    foreach($doc->getElementsByTagName('table') as $table){
        var_dump($table);
    }
?>

The line libxml_use_internal_errors(true); makes sure there are no errors thrown when the html is loaded. As nav and section tags are not supported as "correct" html for instance.

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

1 Comment

Finally! I'm still learning php dom and tried many convoluted examples but this one was simple enough for me to successfully adapt to my needs.

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.