要素のすべての属性を取得するにはどうすればよいですか?以下の例のように、一度に1つしか取得できないので、アンカータグのすべての属性を引き出したいと思います。
$dom = new DOMDocument();
@$dom->loadHTML(http://www.example.com);
$a = $dom->getElementsByTagName("a");
echo $a->getAttribute('href');
ありがとう!
要素のすべての属性を取得するにはどうすればよいですか?以下の例のように、一度に1つしか取得できないので、アンカータグのすべての属性を引き出したいと思います。
$dom = new DOMDocument();
@$dom->loadHTML(http://www.example.com);
$a = $dom->getElementsByTagName("a");
echo $a->getAttribute('href');
ありがとう!
$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);
サイモンの答えに「触発された」。電話を切ることができると思うgetAttribute
ので、電話を使わない解決策を次に示します。
$attrs = array();
for ($i = 0; $i < $a->attributes->length; ++$i) {
$node = $a->attributes->item($i);
$attrs[$node->nodeName] = $node->nodeValue;
}
var_dump($attrs);
$a = $dom->getElementsByTagName("a");
foreach($a as $element)
{
echo $element->getAttribute('href');
}
$html = $data['html'];
if(!empty($html)){
$doc = new DOMDocument();
$doc->loadHTML($html);
$doc->saveHTML();
$datadom = $doc->getElementsByTagName("input");
foreach($datadom as $element)
{
$class =$class." ".$element->getAttribute('class');
}
}