Home  >  Article  >  Backend Development  >  PHP uses regular expressions to match the types and attribute values ​​of all elements in the form

PHP uses regular expressions to match the types and attribute values ​​of all elements in the form

高洛峰
高洛峰Original
2017-03-01 16:27:341409browse

前言

最近工作中遇到一个需求,需要在正则匹配页面中,所有可能存在的 form 表单的元素,可能有 input,action,select,textarea等等所有可能的元素,本文给出一个代码示例。感兴趣的朋友们可以参考学习。

实例代码如下

假设页面 1.html 的网页源代码是:

<!DOCTYPE html>
<html>
 <head>
 <meta charset="utf-8" />
 <title>一个含有 form 表单的登录注册页面</title>
 <style type="text/css">
 *{padding:0;margin:0;font-size:12px;}
 .tbl{margin:20px auto;border:1px solid #AACCEE;padding:30px 30px;border-radius:4px;}
 .tbl td{line-height:30px;vertical-align:top;}
 textarea{resize:none;height:60px;}
 </style>
 </head>
 <body>
 <form action="register.php" method="post">
 <input type="hidden" name="cookie" value="SJKjki80KJ8jkl2" />
 <table cellpadding="0" cellspacing="0" class="tbl"> 
 <tr>
 <th colspan="2">注册</th>
 </tr>
 <tr>
 <td>昵称:</td>
 <td><input type="text" name = &#39;name&#39; required="" /></td>
 </tr>
 <tr>
 <td>密码:</td>
 <td><input name="password" type="password" required="required" /></td>
 </tr>
 <tr>
 <td>年龄:</td>
 <td><input name="age" value="22" required="required" ></td>
 </tr>
 <tr>
 <td>性别:</td>
 <td>
 <input type="radio" name="sex" value="1" /> 男
 <input type="radio" name="sex" value="0" /> 女
 </td>
 </tr>
 <tr>
 <td>地区:</td>
 <td>
 <select name="area">
 <option value="jiangsu">江苏</option>
 <option value="shandong">山东</option>
 <option value="fujian">福建</option>
 <option value="beijing">北京</option>
 </select>
 </td>
 </tr>
 <tr>
 <td>城市:</td>
 <td>
 <select name="city">
 <option value="qingdao">青岛</option>
 <option value="longyan">龙岩</option>
 <option value="beijing">北京</option>
 <option value="wuxi">无锡</option>
 </select>
 </td>
 </tr>
 <tr>
 <td>兴趣:</td>
 <td>
 <input type="checkbox" name="xingqu[]" value="1" > 篮球
 <input type="checkbox" name="xingqu[]" value="2"> 足球
 <input type="checkbox" name="xingqu[]" value="3" > 跳高
 </td>
 </tr>
 <tr>
 <td>照片:</td>
 <td><input type="file" name="photo" /></td>
 </tr>
 <tr>
 <td>简介:</td>
 <td><textarea name="summary" required="">这里填入个人简介</textarea></td>
 </tr>
 <tr>
 <td>备注:</td>
 <td><textarea name="remark" required="">1</textarea ></td>
 </tr>
 <tr>
 <td colspan="2"><input type="submit" name="register" value="注册"></td>
 </tr>
 </table> 
 </form>
 <form action="login.php" method="post">
 <table cellpadding="0" cellspacing="0" class="tbl">
 <tr>
 <th colspan="2">登录</th>
 </tr>
 <tr>
 <td>昵称:</td>
 <td><input type="text" name = &#39;name&#39; required="required" /></td>
 </tr>
 <tr>
 <td>密码:</td>
 <td><input name="password" type="password" required="required" /></td>
 </tr>
 <tr>
 <td>备注:</td>
 <td><textarea name="remark" required="">2</textarea></td>
 </tr> 
 <tr>
 <td colspan="2"><input type="submit" name="register" value="登录"></td>
 </tr>
 </table> 
 </form> 
 </body>
</html>

我们需要获取到这个页面所有 form 表单,及每个 form 表单所包含的各类表单元素,例如:input,select,textarea等等。

匹配的源代码是:

$content = file_get_contents(&#39;1.html&#39;);
$arr_form = get_page_form_data($content);
if(empty($arr_form))
{
 echo &#39;抱歉!未匹配到 form 表单元素&#39;;
}else
{
 foreach($arr_form as $k => $v)
 {
 echo &#39;form&#39;.($k+1).&#39;:<br />&#39;;
 if(!empty($v[&#39;action&#39;]))
 {
 echo &#39;----action:<br />&#39;;
 echo &#39;--------&#39;.$v[&#39;action&#39;].&#39;<br />&#39;;
 }
 if(!empty($v[&#39;method&#39;]))
 {
 echo &#39;----method:<br />&#39;;
 echo &#39;--------&#39;.$v[&#39;method&#39;].&#39;<br />&#39;;
 } 
 if(!empty($v[&#39;inputs&#39;]))
 {
 echo &#39;----inputs:<br />&#39;;
 foreach($v[&#39;inputs&#39;] as $key => $value)
 {
 echo &#39;--------name:&#39;.$value[&#39;name&#39;].&#39; type:&#39;.$value[&#39;type&#39;].&#39; value:&#39;.$value[&#39;value&#39;].&#39;<br />&#39;;
 }
 } 
 if(!empty($v[&#39;textarea&#39;]))
 {
 echo &#39;----textarea:<br />&#39;;
 foreach($v[&#39;textarea&#39;] as $key => $value)
 {
 echo &#39;--------name:&#39;.$value[&#39;name&#39;].&#39; value:&#39;.$value[&#39;value&#39;].&#39;<br />&#39;;
 }
 } 
 if(!empty($v[&#39;select&#39;]))
 {
 echo &#39;----select:<br />&#39;;
 for($m = 0;$m < count($v[&#39;select&#39;]);$m ++)
 {
 echo &#39;--------name:&#39;.$v[&#39;select&#39;][$m][&#39;name&#39;].&#39;<br />&#39;;
 if(!empty($v[&#39;select&#39;][$m][&#39;option&#39;]))
 {
 foreach ($v[&#39;select&#39;][$m][&#39;option&#39;] as $key => $value) 
 {
 echo &#39;------------value:&#39;.$value.&#39;<br />&#39;;
 }
 }
 }
 }
 }
}
// 获取页面中的 form 表单中的所有 input、textarea 元素中 name、value、type 等属性值
function get_page_form_data($content)
{
 $arr_form = array();
 $form = regular_form_tags($content);
 for($i = 0;$i < count($form[0]);$i ++)
 {
 $arr_form[$i][&#39;action&#39;] = regular_form_action($form[1][$i]);
 $arr_form[$i][&#39;method&#39;] = regular_form_method($form[1][$i]);
 $input = regular_input_tags($form[2][$i]);
 for($j = 0;$j < count($input[0]);$j ++)
 {
 $arr_form[$i][&#39;inputs&#39;][$j][&#39;name&#39;] = regular_input_name($input[0][$j]);
 $arr_form[$i][&#39;inputs&#39;][$j][&#39;type&#39;] = regular_input_type($input[0][$j]);
 $arr_form[$i][&#39;inputs&#39;][$j][&#39;value&#39;] = regular_input_value($input[0][$j]);
 }
 $textarea = regular_textarea_tags($form[2][$i]);
 for($k = 0;$k < count($textarea);$k ++)
 {
 $arr_form[$i][&#39;textarea&#39;][$k][&#39;name&#39;] = regular_textarea_name($textarea[$k]);
 $arr_form[$i][&#39;textarea&#39;][$k][&#39;value&#39;] = regular_textarea_value($textarea[$k]);
 }
 $select = regular_select_tags($form[2][$i]);
 for($l = 0;$l < count($select[0]);$l ++)
 {
 $arr_form[$i][&#39;select&#39;][$l][&#39;name&#39;] = regular_select_name($select[1][$l]);
 $option = regular_option_tags($select[2][$l]);
 for($n = 0;$n < count($option[$l]);$n ++)
 {
 $arr_form[$i][&#39;select&#39;][$l][&#39;option&#39;][$n] = regular_option_value($option[$l][$n]);
 } 
 }
 }
 return $arr_form;
}
// 正则匹配 form 标签
function regular_form_tags($string)
{ 
 $pattern = &#39;/<form(.*?)>(.*?)<\/form>/si&#39;; 
 preg_match_all($pattern,$string,$result);
 return $result;
}
// 正则匹配 form 标签的 action 属性值
function regular_form_action($string)
{
 $pattern = &#39;/action[\s]*?=[\s]*?([\&#39;\"])(.*?)\1/&#39;;
 if(preg_match($pattern,$string,$result))
 {
 return $result[2];
 }
 return null;
}
// 正则匹配 form 标签的 method 属性值
function regular_form_method($string)
{
 $pattern = &#39;/method[\s]*?=[\s]*?([\&#39;\"])(.*?)\1/&#39;;
 if(preg_match($pattern,$string,$result))
 {
 return $result[2];
 }
 return null;
}
// 正则匹配 input 标签
function regular_input_tags($string)
{
 $pattern = &#39;/<input.*?\/?>/si&#39;; 
 if(preg_match_all($pattern,$string,$result))
 {
 return $result; 
 }
 return null;
}
// 正则匹配 input 标签的 name 属性值
function regular_input_name($string)
{
 $pattern = &#39;/name[\s]*?=[\s]*?([\&#39;\"])(.*?)\1/&#39;;
 if(preg_match($pattern,$string,$result))
 {
 return $result[2];
 }
 return null;
 
}
// 正则匹配 input 标签的 type 属性值
function regular_input_type($string)
{
 $pattern = &#39;/type[\s]*?=[\s]*?([\&#39;\"])(.*?)\1/&#39;;
 if(preg_match($pattern,$string,$result))
 {
 return $result[2];
 }
 return null;
}
// 正则匹配 input 标签的 value 属性值
function regular_input_value($string)
{
 $pattern = &#39;/value[\s]*?=[\s]*?([\&#39;\"])(.*?)\1/&#39;;
 if(preg_match($pattern,$string,$result))
 {
 return $result[2];
 }
 return null; 
}
// 正则匹配 textarea 标签
function regular_textarea_tags($string)
{
 $pattern = &#39;/(<textarea.*?>.*?<\/textarea[\s]*?>)/si&#39;; 
 if(preg_match_all($pattern,$string,$result))
 {
 return $result[1];
 }
 return null;
}
// 正则匹配 textarea 标签的 name 属性值
function regular_textarea_name($string)
{
 $pattern = &#39;/name[\s]*?=[\s]*?([\&#39;\"])(.*?)\1/si&#39;; 
 if(preg_match($pattern,$string,$result))
 {
 return $result[2];
 }
 return null;
}
// 正则匹配 textarea 标签的 name 属性值
function regular_textarea_value($string)
{
 $pattern = &#39;/<textarea.*?>(.*?)<\/textarea>/si&#39;; 
 if(preg_match($pattern,$string,$result))
 {
 return $result[1];
 }
 return null;
}
// 正则匹配 select 标签
function regular_select_tags($string)
{
 $pattern = &#39;/<select(.*?)>(.*?)<\/select[\s]*?>/si&#39;;
 preg_match_all($pattern,$string,$result);
 return $result;
}
// 正则匹配 select 标签的 option 子标签
function regular_option_tags($string)
{
 $pattern = &#39;/<option(.*?)>.*?<\/option[\s]*?>/si&#39;;
 preg_match_all($pattern,$string,$result);
 return $result; 
}
// 正则匹配 select 标签的 name 属性值
function regular_select_name($string)
{
 $pattern = &#39;/name[\s]*?=[\s]*?([\&#39;\"])(.*?)\1/si&#39;;
 if(preg_match($pattern,$string,$result))
 {
 return $result[2];
 }
 return null;
}
// 正则匹配 select 的子标签 option 的 value 属性值
function regular_option_value($string)
{
 $pattern = &#39;/value[\s]*?=[\s]*?([\&#39;\"])(.*?)\1/si&#39;;
 if(preg_match($pattern,$string,$result))
 {
 return $result[2];
 }
 return null;
}

运行效果如下图所示:

PHP uses regular expressions to match the types and attribute values ​​of all elements in the form

PHP uses regular expressions to match the types and attribute values ​​of all elements in the form

这样我们就可以实现获取任意一个页面中的 form 表单所有存在的元素啦!

更多PHP uses regular expressions to match the types and attribute values ​​of all elements in the form相关文章请关注PHP中文网!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn