2

I want to parse the HTML data from a particular url provided. But i am not able to parse data as i am getting few errors.

This is my code :-

    $html = new DOMDocument();
    $html->loadHTML($url);  //$url is where the site url is defined
    $value = array();
    foreach($html->find($identifier) as $element) //$identifier is where div, a etc is stored
    {
        $value[] = $element->src."<br />";
    }
    print_r($value);

I am getting the following error

Call to undefined method DOMDocument::find()    

Can anyone help me out with this .I am using cake-php 2.0

2
  • What is a specific example of identifier? Commented Mar 1, 2013 at 7:07
  • for example i take $identifier = div Commented Mar 1, 2013 at 7:08

4 Answers 4

2

As the error indicates, DOMDocument has no find method. However, it does have a lot of very nice DOM parsing methods: http://www.php.net/manual/en/class.domdocument.php

If $identifier is an element name, it would make sense to use

foreach ($html->getElementsByTagName($identifier) as $element) {
    $value[] = $element->nodeValue + "<br>";
}
Sign up to request clarification or add additional context in comments.

Comments

1

This will not work:

$html->loadHTML($url);

LoadHTML loads a string, this will work for example:

$html->loadHTML("<html><body>Test<br></body></html>");

Try using loadHTMLfile:

$html->loadHTMLFile("filename.html");

See: http://www.php.net/manual/en/domdocument.loadhtmlfile.php

Comments

1

How about reading the documentation on the class you're using? DOMDocument is well documented on the php.net site and has no mention of a find method, why would you presume it does?

DOMDocument has a lot of methods that are familiar to any JavaScript developer, such as getElementsByTagName and getElementById which should be what you're looking for.

Also, if you're loading the HTML from a URL, the DOMDocument::loadHTML method won't work, as it expects a string containing valid HTML, not a URI.

DOMDocument::loadHTMLFile might work with a URL parameter (can't be sure, don't have PHP installed on this computer to check), but if it doesn't you can combine DOMDocument::loadHTML and file_get_contents to get it to work.

Comments

0

As far as I can see the cake php documentation ... there is no find() function for domDocument Class.

How about using toArray() method of Xml class documented here

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.