本文介绍了获取html输入元素的值作为php字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在php中有一个作为字符串加载的html文件,我需要获取HTML字符串中输入元素的值.有人可以帮我建立一个使用输入元素名称并返回其值的函数吗?
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应该为123456.
$val = getVal("command"); //val should be 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;更多推荐
获取html输入元素的值作为php字符串
发布评论