Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPath get attribute value in PHP [duplicate]

Tags:

php

xpath

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

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']"); 

How do I get value to be "need to get this"?

Update:

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'); 
like image 795
jerrymouse Avatar asked Nov 06 '11 13:11

jerrymouse


1 Answers

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.

like image 58
Martin Honnen Avatar answered Oct 15 '22 18:10

Martin Honnen