Home >Backend Development >PHP Tutorial >Application of regular expressions in PHP

Application of regular expressions in PHP

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-29 08:58:51886browse

This program implements a demo that uses regular expressions to implement login verification

<1>:
<?php 
if(isset($_POST["sub"])){
	$text=$_POST["text"];
	$patten=&#39;^[0-9]*$&#39;;
	if(!preg_match($patten,$text,$x)){
	echo"<script>alert('用户没有输入数字');</script>";
}else{
		if($x<1){
			$y=$x;
			echo "y=".$y."<br>";
		}else if($x<10){
			$y=2*$x-1;
			echo
			 "y=".$y."<br>";
		}else{
			$y=3*$x-11;
			echo "y=".$y."<br>";
		}	
}
?>
<html>
<head>
</head>
<body>
<form method=&#39;post&#39;>
请输入信息:<input type="text"name="text">
<input type="submit"name="sub"value="提交">

</form>
</body>
</html>



<2>:
<html>
<head>
</head>
<body>
<form method=&#39;post&#39;>
注册账号:<input type="text"name="aNum"><br>
登录密码:<input type="password"name="pwd"><br>
重复密码:<input type="password"name="rPwd"><br>
邮箱地址:<input type="text"name="email"><br>
手机号码:<input type="text"name="tel"><br>
<input type="submit"name="sub"value="注册">
<?php 
if(isset($_POST["sub"])){
	$aNum=$_POST["aNum"];
	$pwd=$_POST["pwd"];
	$rPwd=$_POST["rPwd"];
	$email=$_POST["email"];
	$tel=$_POST["tel"];
	$patten1="^\w+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$";//验证邮箱
	$patten2="[0-9]{11}";//11位数字组成,验证手机号码
	$patten3="[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*"//验证账号
	if(!preg_match($patten3,$aNum)){
		echo"<script>alert('账号格式不对');</script>";
	}else{
		if($pwd.length<6){
			echo"<script>alert('密码格式不对');</script>";
		}else{
			if(!preg_match($patten,$email)){
				echo"<script>alert('email格式不正确');</script>";
			}else{
			if(!preg_match($patten2,$tel)){
				echo"<script>alert('手机号码格式不正确');</script>";
			}else{
				if(strlen($pwd)!=strlen($rPwd)){
					echo"<script>alert('两次密码不一致');</script>";
				}else{
					echo"用户您好!您的账号为:".$aNum.",密码为:".$pwd.",邮箱为:".
					$email.",手机号码为:".$tel;
				}
			}
		}
	}
}	

?>
</form>
</body>
</html>


The above has introduced the application of regular expressions in PHP, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

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