2

I need take the attribute value using xpath, I tried several times but always get null.

Here is my html code:

<input type="hidden" id="Lat" name="myMap" value="22.445575, -18.722164">

Code in php:

$val= $xpath->query('//input[@type="hidden" and @name="myMap"]/@value' );

$list=array();              

    foreach($val as $v){
       $list[]=$v->nodeValue;
      }

var_dump($list);
$sValue= (string)$val[0];

Notes:

  • I've tried with other xpath in the same url and it works, but not with hidden input
  • I've tried with $v->item(0); or item(0)->nodeValue; and they always produce the same result
4

2 回答 2

3

这是您使用 DOM xpath 的代码:

$html='<input type="hidden" id="Lat" name="myMap" value="22.445575, -18.722164">';
$doc = new DOMDocument;
$doc->loadHTML($html);
$xpath = new DOMXpath($doc);
$val= $xpath->query('//input[@type="hidden" and @name = "myMap"]/@value' );
$list=array();              
    foreach($val as $v){
       $list[]=$v->nodeValue;
      }
var_dump($list);
于 2015-06-09T09:31:31.520 回答
0

我使用 PHP Simple HTML DOM Parser 为您找到了一个解决方案:

不要忘记下载这个库: http ://simplehtmldom.sourceforge.net/

include("simple_html_dom.php");
$html=str_get_html('<input type="hidden" id="Lat" name="myMap" value="22.445575, -18.722164">');
$input_val=$html->find('input[type=hidden],[name=myMap]',0)->getAttribute("value");
echo $input_val;

如果您想从 url 获取 html 更改 str_get_html 与 file_get_html :

$html=file_get_html("http://www.example.com/page");
于 2015-06-08T23:45:31.057 回答