使用DOMXPath提取隐藏的输入元素的属性值 [英] Extract attribute value of a hidden input element using DOMXPath

查看:384
本文介绍了使用DOMXPath提取隐藏的输入元素的属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段HTML代码:

I have a got piece of HTML code:

<form method="post" action="/">
  <input type="hidden" name="example-name" value="example-value">
  <button type="submit">Submit</button>
</form>

如何使用PHP中的DOMXPath提取隐藏输入的值?
我已经尝试过这样的一个例子:

How can I extract value of the hidden input using DOMXPath in PHP? I have tried somethig like this:

//$site - the html code
$doc = new DOMDocument();
$doc->loadHTML($site);
$xpath = new DOMXpath($doc);

$kod = $xpath->query("//input[@name='example-name']");
foreach($kod as $node)
$values[]=$node->nodeValue;
return $values;

但它返回一个空数组。错误在哪里?

But it returns an empty array. Where is the mistake?

推荐答案

尝试这样获取输入元素与名称属性 example-name

Try this to get the value attribute of the input element with the name attribute example-name

'//input[@name="example-name"]/@value'

结果

Array
(
    [0] => example-value
)

您的XPath没有选择属性轴(我认为这就是所谓的),但文本轴,由于输入没有文本,数组中的值为空。它确实找到了元素。

Your XPath didn't select the attribute axis (I think that's what it's called) but the text axis and since input has no text, the value in the array was empty. It did find the element though.

这篇关于使用DOMXPath提取隐藏的输入元素的属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆