Sunday 31 March 2019

dom - php DomDocument: how can i print an attribute of an element?



how do I print the an attribute of an element?



example:




$doc = new DOMDocument();
@$doc->loadHTML($page);
$xpath = new DOMXPath($doc);
$arts= $xpath->query("/td");

foreach ($arts as $art) {
here i wanna print the attribute class of the td element, how do i do so ?
}

Answer




use DOMElement::getAttribute



$art->getAttribute('class');


also, simpleHTMLDOM is more suitable for dealing with html:



$html = str_get_html($page);
foreach($html->find('td') as $element)
echo $element->class.'
';

}

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...