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;
$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:

$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) {
  $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

Kommentar verfassen

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

Nach oben scrollen