3

I can get the source code fine, but I now want to be able to get the data from within a specific div:

$html = file_get_contents('http://www.website.com');

say $html contains:

<div class="productData">
   <div class="productDescription">Here is the product description</div>
   <div class="productPrice">1.99</div>
</div>

I want to be able to return the data within , and do this for all occurrences?

Thank you.

1 Answer 1

2

Use the DOMDocument class, combined with DOMXPath, something like this:

$url = 'http://www.website.com/';
$dom = new DOMDocument();
$dom->load($url);
$xpath = new DOMXPath($dom);
$nodes = $xpath->query("//*[contains(@class, 'productData')]");
foreach ($nodes as $node) {
    // do something
}
Sign up to request clarification or add additional context in comments.

12 Comments

either do this or use preg_match function for matching the strings and manipulating
I did try something along these lines, but I get this: Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: Unexpected end tag : head in Entity
What if you use the revised code (change $url to whatever you want to use)
I get: Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: htmlParseEntityRef: expecting ';' in Entity Could this be anything to do with the page containing javascript?
|

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.