Considering you have your node as a DOMElement
or DOMNode
, you can use the $attributes property of the DOMNode
class : it contains a list of the attributes that the node has.
Using that property, you can loop over the attributes, getting the name and value of each one, with their $nodeName
and $nodeValue
properties.
For instance, in your case, you could use something like this :
$str = <<<STR
<p align=center style="font-size: 12px;">some text</p>
<a href="#" target="_blank">some link<a/>
STR;
$dom = new DOMDocument();
$dom->loadHTML($str);
$p = $dom->getElementsByTagName('p')->item(0);
if ($p->hasAttributes()) {
foreach ($p->attributes as $attr) {
$name = $attr->nodeName;
$value = $attr->nodeValue;
echo "Attribute '$name' :: '$value'<br />";
}
}
Which would get you this kind of output :
Attribute 'align' :: 'center'
Attribute 'style' :: 'font-size: 12px;'
i.e. we have the two attributes of the node, without knowing their names before ; and for each attribute, we can obtain its name and its value.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…