DOMDocument& XPath - 每个节点的HTML标签 [英] DOMDocument & XPath - HTML Tag of each Node
本文介绍了DOMDocument& XPath - 每个节点的HTML标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
鉴于以下PHP代码,使用 DOMDocument
:
Given the following PHP code using DOMDocument
:
$inputs = $xpath->query('//input | //select | //textarea', $form);
if ($inputs->length > 0)
{
for ($j = 0; $j < $inputs->length; $j++)
{
$input = $inputs->item($j);
$input->getAttribute('name'); // Returns the Attribute
$input->getTag(); // How can I get the input, select or textarea tag?
}
}
如何知道每个匹配节点的标签名称
How can I know the tag name of each matched node?
推荐答案
$inputs = $xpath->query('//input | //select | //textarea', $form);
// no need for "if ($inputs->length > 0) - the for loop won't run if it is 0
for ($j = 0; $j < $inputs->length; $j++)
{
$input = $inputs->item($j);
echo $input->nodeName;
}
请参阅: http://www.php.net/manual/en/class.domnode.php#domnode.props.nodename
PS:除了查看文档, var_dump()
可以真的有帮助。
P.S.: Apart from looking into the docs, a var_dump()
can be really helpful.
这篇关于DOMDocument& XPath - 每个节点的HTML标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文