php dom 获取节点的所有属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2385834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:16:27  来源:igfitidea点击:

php dom get all attributes of a node

phpdom

提问by alin

is there any easy way of getting all attributes of a node without checking if it has that attribute? short, here's an example of what i'm trying to do: i have this short domdocument:

有没有简单的方法来获取节点的所有属性而不检查它是否具有该属性?简而言之,这是我正在尝试做的一个例子:我有这个简短的domdocument:

<p align=center style="font-size: 12px;">some text</p>
<a href="#" target="_blank">some link<a/>

okay.. now if i check p tag with getAttribute('align') i'll get the center value.. that's cool, but i want to see if p tag has also another attribute like style without checking for every attribute possible. on img tag i'll have to check for src, width, height, style, onclick, etc.. to verify if they exists.. but i'm thinking it might be a easier way of seeing all attributes.

好的..现在如果我用 getAttribute('align') 检查 p 标签,我会得到中心值.. 那很酷,但我想看看 p 标签是否还有另一个属性,比如 style 而不检查每个可能的属性。在 img 标签上,我必须检查 src、宽度、高度、样式、onclick 等。以验证它们是否存在。但我认为这可能是查看所有属性的更简单方法。

回答by Pascal MARTIN

Considering you have your node as a DOMElementor DOMNode, you can use the $attributesproperty of the DOMNodeclass : it contains a list of the attributes that the node has.

考虑到您的节点为DOMElementor DOMNode,您可以使用类的$attributes属性DOMNode:它包含节点具有的属性列表。

Using that property, you can loop over the attributes, getting the name and value of each one, with their $nodeNameand $nodeValueproperties.

使用属性,你可以遍历所有的属性,让每个人的名称和值,用自己$nodeName$nodeValue性能。


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.

即我们有节点的两个属性,之前不知道它们的名字;对于每个属性,我们可以获取其名称和值。

回答by Pavunkumar

Use this code, it will give you the specified attributes.

使用此代码,它将为您提供指定的属性。

 

<html>
<script>
function test()
{
getvalue=document.getElementById("iid").getAttribute("align")
alert ( getvalue) ;
}
</script>

<body>
<p id=iid align="center" background="red" onclick="test();" >
This is for testing
php dom get all attributes of a node
</p>
</body>
</html>

Now when you click the conent of the p tag . It will show you align attirbute values.

现在,当您单击 p 标签的内容时。它将显示您对齐属性值。