我有一个 html 文件作为字符串加载到 php 中,我需要获取 HTML 字符串中输入元素的值。有人可以帮助我构建一个函数,该函数采用输入元素的名称并返回其值吗?
这是我想要执行的功能的示例:
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("命令");//val 应为 123456。
有什么伊达吗?谢谢
最佳答案
这是一个 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 ($inputs as $input) {
if ($input->getAttribute("name") == "id") {
$value = $input->getAttribute("value");
}
}
echo $value;
关于php - 获取 html 输入元素的值作为 php 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13591126/