我想使用 id 标签提取属性“value”的值。
我的代码:
<?php
$url = 'http://turni.tt-contact.com/Default.aspx';
$contents = htmlentities(file_get_contents($url));
echo $contents."\n"; //html
$dom = new DOMDocument;
$dom->validateOnParse = true;
$dom->loadHTML($contents);
$dom->preserveWhiteSpace = false;
$data = $dom->getElementById("__VIEWSTATE");
echo $data->nodeValue;
?>
我想要属性“value”->“THIS”:
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="THIS">
但代码仅返回 html 代码。
我需要改变什么?
还可以将其修改为:
$xpath = new DOMXpath($dom);
$data = $xpath->query('//input[@id="__VIEWSTATE"]');
$node = $data->item(0);
echo $node->getAttribute('value');
我收到此错误:
Fatal error: Call to a member function getAttribute() on null
最佳答案
试试这个:
$data->getAttribute('value');
$attrs = array();
for ($i = 0; $i < $data->attributes->length; ++$i){
$node = $data->attributes->item($i);
$attrs[$node->nodeName] = $node->nodeValue;
}
var_dump($attrs);
关于PHP DOM文档 : Get attribute value from id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46669797/