An example with DOMDocument
querying the class attribute value verbatim (with spaces around):
// configuration
libxml_use_internal_errors(true);
// input
$url = 'http://www.amazon.com/Likeable-Social-Media-Irresistible-ebook/dp/B00511ONPG/ref=tmm_kin_title_0?ie=UTF8&qid=1367741120&sr=8-1';
// processing
$doc = new DOMDocument();
$doc->loadHTMLFile($url);
$xpath = new DOMXPath($doc);
$prices = $xpath->query("//*[@class=' price ']/text()");
// output
foreach($prices as $index => $price) {
printf("%d: %s\n", $index, trim($price->textContent));
}
Output:
0: $14.81
1: $18.38
2: $11.58
3: --
4:
5:
Please note that the URL you gave contains invalid HTML. Therefore the simpledom parser might produce different results (or does not work at all) with the data provided. This is equally true for the DOMDocument
object I use here, however, it is build on top of the pretty stable libxml library (not only used in the PHP world, but in very many other worlds as well) and it also has a recovery
property which allows further control.