XPath在PHP中获取属性值 [英] XPath get attribute value in PHP

查看:98
本文介绍了XPath在PHP中获取属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
如何从XML提取节点属性使用PHP的DOM解析器

Possible Duplicate:
How to extract a node attribute from XML using PHP's DOM Parser

如何提取HTML标记值?

How do I extract an HTML tag value?

HTML:

<input type="hidden" name="text1" id="text1" value="need to get this">

PHP:

$homepage = file_get_contents('http://www.example.com');
$doc = new DOMDocument;
$doc->preserveWhiteSpace = false;
@$doc->loadHTML($homepage);
$xpath = new DOMXpath($doc);
$filtered = $xpath->query("//input[@name='text1']");

如何使value成为需要此" ?

更新:

我成功了,希望它也能对其他人有所帮助.在上面的代码之后,我得到的值是:

I got it working and hope it will help others too. After above code I got the value by:

echo $filtered->item(0)->getAttribute('value');

推荐答案

XPath可以通过$xpath->query("//input[@name='text1']/@value");获取值属性.然后,您可以遍历属性节点的节点列表,并访问每个属性节点的$value属性.

XPath can do the job of getting the value attribute with $xpath->query("//input[@name='text1']/@value");. Then you can iterate over the node list of attribute nodes and access the $value property of each attribute node.

这篇关于XPath在PHP中获取属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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