Php Domdocument Check Span Class
Solution 1:
Your HTML would give error of Input is not proper UTF-8, indicate encoding ! Bytes: 0xE0 0x20 0x6D 0x65
if you use $doc->load("file.html");
here is a simple work around
$doc = new DOMDocument('1.0', 'UTF-8');
libxml_use_internal_errors(true);
$doc->loadHTML(file_get_contents("file.html"));
foreach ( $doc->getElementsByTagName('span') as $node ) {
if (preg_match("/^font1[7|8]$/", $node->getAttribute('class'))) {
echo $node->nodeValue, "<br /><br />";
}
}
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 ($nodes as $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")]');
Post a Comment for "Php Domdocument Check Span Class"