2

I am trying to parse a table and output the a plaintext in another table. I have gotten this far:

<?php 
    if (url_exists($url))
    {
        $html = file_get_html($url);
    }
    else 
    {
        echo "URL doesn't exist.";
    }

    if ($html && is_object($html) && isset($html->nodes))
    {
        // Everything checks out
        $table = $html->find('table[border]');
        if (!empty($table))
        {
        $row = $table->find('tr');
        }
    }
    else 
    {
        echo "Fetched page is not ok.";
    }
?>

This returns an error: Fatal error: Call to a member function find() on a non-object in /var/www/html/jsudimak/mailman/webdev-test1.php on line 78

Line 78 is this one: $row = $table->find('tr');

This means that :

  • the html is valid
  • the table I am trying to parse is also valid

Therefore, I am bewildered by the fact that the find() method is still returning this error.

I have looked into the cause of this error extensively for the past few days and I have yet to find a solution. I have also tried some other parsing tools no still no luck. Help me with this fellow debuggers!!!!

By the way, I am using the Simple HTML Dom Parser to parse the table.

6
  • Have you tried using $row = $table->find('tr')[0]; ? Commented Mar 9, 2016 at 14:50
  • 1
    @Nadir and why by this way it should not get error? Commented Mar 9, 2016 at 14:51
  • $html->find('table[border]'); and $table->find('tr'); is both these find function in same class ? Commented Mar 9, 2016 at 14:52
  • 1
    @fusion3k my wrong, I meant $table = $html->find('table[border]')[0]; The documentation says that, unless you specify an index in the function find(), it will return an array Commented Mar 9, 2016 at 14:53
  • 1
    ^ this is a good catch! Commented Mar 9, 2016 at 14:54

1 Answer 1

2

Use $table = $html->find('table[border]')[0];

The documentation says that, unless you specify an index in the function find(), it will return an array

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

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.