Get The Value Of A Html Input Element As A Php String
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 th
Solution 1:
Here is an example with DOM:
$html="<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>";
$document= new DOMDocument();
$document->loadHTML($html);
$inputs=$document->getElementsByTagName("input");
foreach ($inputsas$input) {
if ($input->getAttribute("name") =="id") {
$value=$input->getAttribute("value");
}
}
echo $value;
Solution 2:
Load the HTML in a DOMDocument
, then a simple XPath query would to the trick:
$xpath = new DOMXPath($domDocument);
$items = $xpath->query('//*[@name="command"]/@value');
if ($items->length)
$value = $items->item(0)->value;
Post a Comment for "Get The Value Of A Html Input Element As A Php String"