Skip to content Skip to sidebar Skip to footer

Php Domdocument Check Span Class

How can I iterate all tag and check if class is font18 or font17? $html = new DOMDocument(); $html->load('file.html'); html:

<

Solution 1:

Solution 2:

The follwing will loop through all span tags and you can use this to check the class (if the HTML snippet you provided is indeed the one you are using):

$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->load('file.html');

$xpath = new DOMXPath($doc);
$nodes = $xpath->query('//span');

foreach ($nodesas$node) {
    echo$node->getAttribute('class');
}

Demo: http://codepad.viper-7.com/pQuQw1

If the HTML is actually different you can tell me so I can change my snippet. It may also be worthwhile to only select specific elements in the xpath query (e.g. to only select elements with class font17 or font18) .

Note that I have used DOMXPath because this will give you more flexibility to change the query to select the elements you need depending on your HTML

If you only want to select elements with class font17 or font18 you can change the query to something like:

$nodes = $xpath->query('//span[contains(@class, "font17")]|//span[contains(@class, "font18")]');

Demo: http://codepad.viper-7.com/mHo5P7

Post a Comment for "Php Domdocument Check Span Class"