PHP XPATH – Select multiple classes

How to select multiple css classes in PHP XPATH:

PHP XPATH - Select multiple classes

To parse the marked line at the image you want to filter for „col-xs-5 col-sm3“. Attention: They have to stay together! With this method you can’t parse for a css hierarchy! The values have to stay in one line!

$dom = new DomDocument;
$dom->loadHTMLFile($url);
$xpath = new DomXPath($dom);
$nodes = $xpath->query("//div[contains(@class, 'col-xs-5') and contains(@class, 'col-sm-4')]");

If you have just one value you can print it out:

print_r($nodes[0]);
$price = $nodes[0]->nodeValue;

If you have multiple occurrences you can loop them or select the right one with the method above:

foreach ($nodes as $i => $node) {
  print_r($node);
  $price = $node->nodeValue;
}

With the parsed value (nodeValue or textContent) you are able to store the contents of the html markup into sql databases:

PHP XPATH Examples

https://www.php.net/manual/de/domxpath.query.php

Kommentar verfassen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Nach oben scrollen