


PHP uses regular expressions to match the types and attribute values of all elements in the form
前言
最近工作中遇到一个需求,需要在正则匹配页面中,所有可能存在的 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 = 'name' 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 = 'name' 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('1.html'); $arr_form = get_page_form_data($content); if(empty($arr_form)) { echo '抱歉!未匹配到 form 表单元素'; }else { foreach($arr_form as $k => $v) { echo 'form'.($k+1).':<br />'; if(!empty($v['action'])) { echo '----action:<br />'; echo '--------'.$v['action'].'<br />'; } if(!empty($v['method'])) { echo '----method:<br />'; echo '--------'.$v['method'].'<br />'; } if(!empty($v['inputs'])) { echo '----inputs:<br />'; foreach($v['inputs'] as $key => $value) { echo '--------name:'.$value['name'].' type:'.$value['type'].' value:'.$value['value'].'<br />'; } } if(!empty($v['textarea'])) { echo '----textarea:<br />'; foreach($v['textarea'] as $key => $value) { echo '--------name:'.$value['name'].' value:'.$value['value'].'<br />'; } } if(!empty($v['select'])) { echo '----select:<br />'; for($m = 0;$m < count($v['select']);$m ++) { echo '--------name:'.$v['select'][$m]['name'].'<br />'; if(!empty($v['select'][$m]['option'])) { foreach ($v['select'][$m]['option'] as $key => $value) { echo '------------value:'.$value.'<br />'; } } } } } } // 获取页面中的 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]['action'] = regular_form_action($form[1][$i]); $arr_form[$i]['method'] = regular_form_method($form[1][$i]); $input = regular_input_tags($form[2][$i]); for($j = 0;$j < count($input[0]);$j ++) { $arr_form[$i]['inputs'][$j]['name'] = regular_input_name($input[0][$j]); $arr_form[$i]['inputs'][$j]['type'] = regular_input_type($input[0][$j]); $arr_form[$i]['inputs'][$j]['value'] = regular_input_value($input[0][$j]); } $textarea = regular_textarea_tags($form[2][$i]); for($k = 0;$k < count($textarea);$k ++) { $arr_form[$i]['textarea'][$k]['name'] = regular_textarea_name($textarea[$k]); $arr_form[$i]['textarea'][$k]['value'] = regular_textarea_value($textarea[$k]); } $select = regular_select_tags($form[2][$i]); for($l = 0;$l < count($select[0]);$l ++) { $arr_form[$i]['select'][$l]['name'] = regular_select_name($select[1][$l]); $option = regular_option_tags($select[2][$l]); for($n = 0;$n < count($option[$l]);$n ++) { $arr_form[$i]['select'][$l]['option'][$n] = regular_option_value($option[$l][$n]); } } } return $arr_form; } // 正则匹配 form 标签 function regular_form_tags($string) { $pattern = '/<form(.*?)>(.*?)<\/form>/si'; preg_match_all($pattern,$string,$result); return $result; } // 正则匹配 form 标签的 action 属性值 function regular_form_action($string) { $pattern = '/action[\s]*?=[\s]*?([\'\"])(.*?)\1/'; if(preg_match($pattern,$string,$result)) { return $result[2]; } return null; } // 正则匹配 form 标签的 method 属性值 function regular_form_method($string) { $pattern = '/method[\s]*?=[\s]*?([\'\"])(.*?)\1/'; if(preg_match($pattern,$string,$result)) { return $result[2]; } return null; } // 正则匹配 input 标签 function regular_input_tags($string) { $pattern = '/<input.*?\/?>/si'; if(preg_match_all($pattern,$string,$result)) { return $result; } return null; } // 正则匹配 input 标签的 name 属性值 function regular_input_name($string) { $pattern = '/name[\s]*?=[\s]*?([\'\"])(.*?)\1/'; if(preg_match($pattern,$string,$result)) { return $result[2]; } return null; } // 正则匹配 input 标签的 type 属性值 function regular_input_type($string) { $pattern = '/type[\s]*?=[\s]*?([\'\"])(.*?)\1/'; if(preg_match($pattern,$string,$result)) { return $result[2]; } return null; } // 正则匹配 input 标签的 value 属性值 function regular_input_value($string) { $pattern = '/value[\s]*?=[\s]*?([\'\"])(.*?)\1/'; if(preg_match($pattern,$string,$result)) { return $result[2]; } return null; } // 正则匹配 textarea 标签 function regular_textarea_tags($string) { $pattern = '/(<textarea.*?>.*?<\/textarea[\s]*?>)/si'; if(preg_match_all($pattern,$string,$result)) { return $result[1]; } return null; } // 正则匹配 textarea 标签的 name 属性值 function regular_textarea_name($string) { $pattern = '/name[\s]*?=[\s]*?([\'\"])(.*?)\1/si'; if(preg_match($pattern,$string,$result)) { return $result[2]; } return null; } // 正则匹配 textarea 标签的 name 属性值 function regular_textarea_value($string) { $pattern = '/<textarea.*?>(.*?)<\/textarea>/si'; if(preg_match($pattern,$string,$result)) { return $result[1]; } return null; } // 正则匹配 select 标签 function regular_select_tags($string) { $pattern = '/<select(.*?)>(.*?)<\/select[\s]*?>/si'; preg_match_all($pattern,$string,$result); return $result; } // 正则匹配 select 标签的 option 子标签 function regular_option_tags($string) { $pattern = '/<option(.*?)>.*?<\/option[\s]*?>/si'; preg_match_all($pattern,$string,$result); return $result; } // 正则匹配 select 标签的 name 属性值 function regular_select_name($string) { $pattern = '/name[\s]*?=[\s]*?([\'\"])(.*?)\1/si'; if(preg_match($pattern,$string,$result)) { return $result[2]; } return null; } // 正则匹配 select 的子标签 option 的 value 属性值 function regular_option_value($string) { $pattern = '/value[\s]*?=[\s]*?([\'\"])(.*?)\1/si'; if(preg_match($pattern,$string,$result)) { return $result[2]; } return null; }
运行效果如下图所示:
这样我们就可以实现获取任意一个页面中的 form 表单所有存在的元素啦!
更多PHP uses regular expressions to match the types and attribute values of all elements in the form相关文章请关注PHP中文网!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.
