我得到了下面的HTML字符串,我需要在其中访问特定id
的值。
字符串如下
<form name="aspnetForm" method="post" action="userview.aspx" id="aspnetForm">
<div>
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__LASTFOCUS" id="__LASTFOCUS" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="test_value" />
</div>
现在为了访问它,我编写了以下代码
$doc = new DOMDocument();
@$doc->loadHTML($str);
$finder = new DomXPath($doc);
$spanner = $finder->query("//*[contains(@id, '__VIEWSTATE')]");
foreach ($spanner as $node)
{
echo $node->nodeValue;
}
其中str
是我的字符串。现在,每次我运行它时,它都会返回一个空白值
答案 0 :(得分:1)
请尝试在此query
部分中对此进行修改
$html = '<form name="aspnetForm" method="post" action="userview.aspx" id="aspnetForm">
<div>
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__LASTFOCUS" id="__LASTFOCUS" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="adsadsadsdsadsd" />
</div>';
$doc = new DOMDocument;
$doc->loadHTML($html);
$xpath = new DOMXpath($doc);
$val= $xpath->query('//input[@type="hidden" and @id = "__VIEWSTATE"]/@value'
);
print_r($val[0]->nodeValue);