title: XPath get attribute value in PHP
body: <blockquote>
    <p><strong>Possible Duplicate:</strong> <br/>
    <a href="https://stackoverflow.com/questions/3993302/how-to-extract-a-node-attribute-from-xml-using-phps-dom-parser">How to extract a node attribute from XML using PHP&#39;s DOM Parser</a></p>
  </blockquote>
  
  
  
  <p>How do I extract an HTML tag value?</p>
  
  <p>HTML:</p>
  
  <pre><code>&lt;input type="hidden" name="text1" id="text1" value="need to get this"&gt;
  </code></pre>
  
  <p>PHP:</p>
  
  <pre><code>$homepage = file_get_contents('http://www.example.com');
  $doc = new DOMDocument;
  $doc-&gt;preserveWhiteSpace = false;
  @$doc-&gt;loadHTML($homepage);
  $xpath = new DOMXpath($doc);
  $filtered = $xpath-&gt;query("//input[@name='text1']");
  </code></pre>
  
  <p>How do I get <code>value</code> to be <em>"need to get this"</em>?</p>
  
  <p><strong>Update:</strong></p>
  
  <p>I got it working and hope it will help others too. After above code I got the value by:</p>
  
  <pre><code>echo $filtered-&gt;item(0)-&gt;getAttribute('value');
  </code></pre>