Как я могу взять весь атрибут элемента? Как на моем примере ниже я могу только добраться по одному, я хочу вытащить весь атрибут тега привязки.
$dom = new DOMDocument();
@$dom->loadHTML(http://www.example.com);
$a = $dom->getElementsByTagName("a");
echo $a->getAttribute('href');
спасибо!
«Вдохновленный» ответом Саймона. Я думаю, вы можете отключить вызов getAttribute
, поэтому вот решение без него:
$attrs = array();
for ($i = 0; $i < $a->attributes->length; ++$i) {
$node = $a->attributes->item($i);
$attrs[$node->nodeName] = $node->nodeValue;
}
var_dump($attrs);
$length = $a->attributes->length;
$attrs = array();
for ($i = 0; $i < $length; ++$i) {
$name = $a->attributes->item($i)->name;
$value = $a->getAttribute($name);
$attrs[$name] = $value;
}
print_r($attrs);
$a = $dom->getElementsByTagName("a");
foreach($a as $element)
{
echo $element->getAttribute('href');
}