Home > Web Front-end > JS Tutorial > body text

How to use regular expressions to complete form verification and file upload verification

php中世界最好的语言
Release: 2018-03-29 14:53:48
Original
1441 people have browsed it

This time I will bring you how to use regular expressions to complete form verification and file upload verification, and how to use regular expressions to complete form verification and file upload verification Precautions What are they? Here are actual cases. Let’s take a look.

Form regular verification is mainly used to filter form submission information to prevent SQL injection (such as login interface). Uploaded files also need to be verified with file name suffix and size. The following is a simple form verification

 header("Content-type:text/html;charset=utf-8");
 $user = isset($_POST[‘user‘])?$_POST[‘user‘]:null;
 $password = isset($_POST[‘password‘])?$_POST[‘password‘]:null;
 $arr = array(‘png‘,‘gif‘,‘jpg‘);
 $uploads = move_uploaded_file($_FILES[‘face‘][‘tmp_name‘],‘uploads/‘.$_FILES[‘face‘][‘name‘]);
 $file = ‘uploads/‘.$_FILES[‘face‘][‘name‘];
 if($uploads){
   echo ‘上传成功‘;
 }
 if(!preg_match("/^[\x{4e00}-\x{9fa5}]+$/u", $user)){
   //正则检查用户名是否为全汉字组成
   echo "用户名只能由纯汉字组成!";
   die;
 }else if(!preg_match("/^[\x{4e00}-\x{9fa5}A-Za-z0-9_]+$/u",$password)){
   //正则检查密码是否含有非法字符
   echo ‘密码不能包含特殊字符!‘;
   die;
 }else if(!in_array(pathinfo($file, PATHINFO_EXTENSION),$arr)){
   echo "文件格式不正确";
   die;
 }else{
   echo ‘允许注册!‘;
 }
Copy after login

Commonly used php regular expressions:

Matching Chinese postal codes: [1-9]\d {5}(?!\d)

Matching ID card: \d{15}|\d{18}

Matching ip address: \d+\.\d+\.\d+ \.\d+

Regular expression matching URL: [a-zA-z]+://[^\s]*

Regular expression matching email address:\ w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*

Regular expression matching Chinese characters: [\u4e00-\u9fa5]

Function:

preg_match(): The first parameter is the regular rule, the second is the string to be verified, and returns a Boolean value

preg_replace (): Replace characters with regular rules in a string

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

How to verify C# with regular expressions

How to use regular expressions to verify the registry

The above is the detailed content of How to use regular expressions to complete form verification and file upload verification. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!