I have a html file loaded as a string in php, and I need to get the values of the input elements in the HTML string. Can someone help me build a function which takes the name of the input element and returns its value?
This is an example of the function I would like to do:
function getVal($name){
$htmlStr = "<form action = \"action.php\"><input type=\"hidden\" name=\"command\" value=\"123456\">
<input type=\"hidden\" name=\"quantity\" value=\"1\">
<input type=\"hidden\" name=\"user_mode\" value=\"1\">
<input type=\"hidden\" name=\"stock\" value=\"-1255303070\">
<input type=\"hidden\" name=\"id\" value="429762082">
<input type=\"hidden\" name=\"pidm\" value=\"2\"></form>";
// I'd like to get the value of $name here probably using preg_match
return $value; //returns 123456
}
$val = getVal("command"); //val should be 123456.
any idas? Thank you
DOMDocumentto parse the HTML string andDOMXPathto query for the value you want. There are zillions of examples here in SO (both for HTML and XML, it's the same most of the time).