Parsing html to find specific input value

I have the following Paypal button code:

<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="ABCDEFG">
<input type="image" src="https://www.paypalobjects.com/en_US/GB/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal – The safer, easier way to pay online.">
<img alt="" border="0" src="https://www.paypalobjects.com/en_GB/i/scr/pixel.gif" width="1" height="1">
</form>

I need to extract the value from the input with name hosted_button_id

This is what I'm trying:

$html = '<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="ABCDEFG">
<input type="image" src="https://www.paypalobjects.com/en_US/GB/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal – The safer, easier way to pay online.">
<img alt="" border="0" src="https://www.paypalobjects.com/en_GB/i/scr/pixel.gif" width="1" height="1">
</form>';

$dom = new DOMDocument();
$dom->loadHTML($html);

$pp_code = $dom->getAttribute('hosted_button_id');

echo "code is $pp_code"; die;

But i get the errors:

Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: error parsing attribute name in Entity, line: 1 in /home/parkview/DB8NP8XA/htdocs/ajax/actions/addEvent.php on line 20

Fatal error: Call to undefined method DOMDocument::getAttribute() in /home/parkview/DB8NP8XA/htdocs/ajax/actions/addEvent.php on line 22

Answers


Explanation about the "Warning" you get:

While malformed HTML should load successfully, this function may generate E_WARNING errors when it encounters bad markup. libxml's error handling functions may be used to handle these errors.

When your HTML markup is bad - you get a warning which you can disable by following this solution: https://stackoverflow.com/a/6090728/998096

Explanation about the "Fatal Error" you get:

There's not such a method "getAttribute" for DOMDocument, you need first to get the Element. The Element has that type of method. For instance:

 $xml = new DOMDocument(); 

// Load the url's contents into the DOM 
$xml->loadHTMLFile($url); 

//Loop through each <a> tag in the dom and add it to the link array 
$link = $xml->getElementsByTagName('a');
$url = $link->getAttribute('href');

Taken from PHP Manual (Comment).

Consider adding an "id" attribute to that specific input field, and use:

$html->getElementById('the_input_id');

add id attribute in that field and you can just use getElementById("hosted_button_id").value for the value of that id


Adding my comment as an answer..

http://www.php.net/manual/en/domdocument.getelementsbytagname.php

As mentioned in my comment, my PHP is lacking, so hopefully this will be of use. This should grab all elements containing "hosted_button_id" as the name. Assuming you only have one, you should be able to grab the first and only element at index 0 in that NodeList.

$dom->getElementsByTagName("hosted_button_id")->item(0); 

This is what I'm doing as can't extract single element value:

$inputs = $dom->getElementsByTagName("input");

foreach ($inputs as $input) {
    if ($input->getAttribute("name") == "hosted_button_id") {
        $pp_code = $input->getAttribute("value");
    }
}

Need Your Help

Understanding discrepancy between POSIX and Linux/glibc sched_* functions

c linux posix scheduling

POSIX XSH 2.8.4 Process Scheduling defines the behavior of scheduling attributes for threads and processes. The sched_* interfaces are specified to affect the scheduling properties of the process, ...

Blurring background image, not text

javascript jquery html css css3

I am using Unslider's plugin on my website, and I want to blur only the image on all browsers and, but not the text.