如何从标签中获取所有属性?
<a href="URL1">TEXT 1</a>
<a href="URL2" rel="rel2">TEXT 2</a>
<a href="URL3" rel="me" target="_blank">TEXT 3</a>
如何使用PHP获得此结果?有没有办法用PHP DOMDocument做到这一点?
Array
(
[0] => Array
(
[href] => URL1
[text] => TEXT 1
)
[1] => Array
(
[href] => URL2
[text] => TEXT 2
[rel] => rel2
)
[2] => Array
(
[href] => URL3
[text] => TEXT 3
[rel] => me
[target] => _blank
)
)
答案 0 :(得分:3)
可以使用DOMNode::attributes
DOMNamedNodeMap
检索节点的属性,但它可以像数组一样使用:
$html = '<a href="URL1">TEXT 1</a><a href="URL2" rel="rel2">TEXT 2</a><a href="URL3" rel="me" target="_blank">TEXT 3</a>';
$output = array();
$dom = new DOMDocument();
$dom->loadHTML($html);
$anchors = $dom->getElementsByTagName('a');
foreach($anchors as $a)
{
$attrList = array();
if($a->hasAttributes())
{
foreach ($a->attributes as $attribute)
{
$attrList[$attribute->nodeName] = $attribute->nodeValue
$attrList['text'] = $a->nodeValue;
}
}
$output[] = $attrList;
}
print_r($output);
<强>输出强>
Array
(
[0] => Array
(
[href] => URL1
[text] => TEXT 1
)
[1] => Array
(
[href] => URL2
[text] => TEXT 2
[rel] => rel2
)
[2] => Array
(
[href] => URL3
[text] => TEXT 3
[rel] => me
[target] => _blank
)
)
答案 1 :(得分:0)
$dom = new DOMDocument();
$dom->loadHTML($the_html);
然后使用DomDocument方法(http://www.php.net/manual/en/class.domdocument.php)
进行解析这样的事情应该有效
$a = $dom->getElementsByTagName('a');
if ($a->hasAttributes()) {
foreach ($a->attributes as $attribute) {
$name = $attribute->nodeName;
$value = $attribute->nodeValue;
echo "Attribute '$name' => '$value'<br />";
}
}