教程分类

Product category

5.4、php案例实战(电话、Email地址、ip地址、编码)验证及文件上传和数据统计

编辑:57自学网 发布日期:2019-11-28 10:14
  • 高清线路
  • 备用线路
  • 图文解说

目录

5.4案例实战

5.4.1验证电话号码

5.4.2验证Email地址

5.4.3验证IP地址

5.4.4统计关键字

5.4.5检测上传文件类型

5.4.6验证邮政编码


视频代码

5.4.1 验证电话号码

<h2>PHP正则表达式验证</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    输入值:
    <input type ="text" name ="text" value ="输入电话号码" onfocus ="this.value=''">
    <br>
    <br>
    <input type="submit" name="submit" value="验 证">
</form>
<?php
// 定义变量并设置为空值
$text = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
	$text = test_input($_POST["text"]);
	if( $text != "" ){
		echo "<h3>您输入的信息如下:</h3>";
		echo $text;
	}else{
		echo "<h3>您输入的信息非法。</h3>";
	}
}
function test_input($data) {
	$data = trim($data);
	$data = stripslashes($data);
	$data = htmlspecialchars($data);
	if(preg_match_all("/(\d{3}-)(\d{8})$|(\d{4}-)(\d{7})$/",$data,$counts)){
		return $data;
	}else{
		return "";
	}   
}
?>

5.4.2 验证Email地址

<h2>PHP正则表达式验证</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    输入电子邮件地址:<br>
    <input name ="text" type ="text" placeholder="xxxxxx@xxx.xx"   onfocus ="this.value=''" value ="">
    <br>
    <br>
    <input type="submit" name="submit" value="验 证">
</form>
<?php
// 定义变量并设置为空值
$text = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
	$text = test_input($_POST["text"]);
	if( $text != "" ){
		echo "<h3>您输入的信息如下:</h3>";
		echo $text;
	}else{
		echo "<h3>您输入的信息非法。</h3>";
	}
}
function test_input($data) {
	$data = trim($data);
	$data = stripslashes($data);
	$data = htmlspecialchars($data);
    if(preg_match("/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/",$data)){
		return $data;
	}else{
		return "";
	}   
}
?>

5.4.3 验证IP地址

<h2>PHP正则表达式验证</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    输入IP地址:
    <input name ="text" type ="text" placeholder="nnn.nnn.nnn.nnn"   onfocus ="this.value=''" value ="">
    <br>
    <br>
    <input type="submit" name="submit" value="验 证">
</form>
<?php
// 定义变量并设置为空值
$text = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
	$text = test_input($_POST["text"]);
	if( $text != "" ){
		echo "<h3>您输入的信息如下:</h3>";
		echo $text;
	}else{
		echo "<h3>您输入的信息非法。</h3>";
	}
}
function test_input($data) {
	$data = trim($data);
	$data = stripslashes($data);
	$data = htmlspecialchars($data);
	$pattern = '/^(?:(?:2[0-4][0-9]\.)|(?:25[0-5]\.)|(?:1[0-9][0-9]\.)|(?:[1-9][0-9]\.)|(?:[0-9]\.)){3}(?:(?:2[0-5][0-5])|(?:25[0-5])|(?:1[0-9][0-9])|(?:[1-9][0-9])|(?:[0-9]))$/';
	//正则匹配ip地址
	//$ip   = '254.21.0.198';
	/*
	11111111.11111111.11111111.11111111
	0-255.0-255.0-255.0-255
	
	1.
	25[0-5]
	2[0-4][0-9]
	1[0-9][0-9]
	[1-9][0-9]
	[0-9]
	\.
	2.
	25[0-5]
	2[0-4][0-9]
	1[0-9][0-9]
	[1-9][0-9]
	[0-9]
		
	*/
	
    if(preg_match($pattern,$data)){
		return $data;
	}else{
		return "";
	}   
}
?>

5.4.4 统计关键字

<?php
$str = "明月几时有?把酒问青天。不知天上宫阙,今夕是何年。我欲乘风归去,又恐琼楼玉宇,高处不胜寒。起舞弄清影,何似在人间?
转朱阁,低绮户,照无眠。不应有恨,何事长向别时圆?人有悲欢离合,月有阴晴圆缺,此事古难全。但愿人长久,千里共婵娟。";
$b = explode("人",$str);
echo "<p style ='red'>";
echo $str."<br>";
echo "</p>";
echo "关键字:人<br>共出现:".(count($b)-1)."次";
?>

5.4.5 检测上传文件类型

<form action ="" method="post">
    <input type="file" name="text"><input type="submit" name="sub" value ="上 传">
</form>
<?php
if($_POST && $_POST['sub']){
	if(preg_match("/.jpg/",strtolower($_POST['text']))){
		echo "上传为JPG类型图片";
	}else if (preg_match("/.png/",strtolower($_POST['text']))){
		echo "上传为PNG类型图片";	
		
	}else if (preg_match("/.gif/",strtolower($_POST['text']))){
		echo "上传为GIF类型图片";	
	}		
	else if(preg_match("/.rar/",strtolower($_POST['text']))){
		echo "上传为压缩包类型";
	}else{
		echo "没有可上传文件,或者是其他文件类型";
	}
}
?>

5.4.6 验证邮政编码

<h2>PHP正则表达式验证</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    输入邮政编码:
    <input name ="text" type ="text" placeholder="邮政编码"   onfocus ="this.value=''" value ="">
    <br>
    <br>
    <input type="submit" name="submit" value="验 证">
</form>
<?php
// 定义变量并设置为空值
$text = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
	$text = test_input($_POST["text"]);
	if( $text != "" ){
		echo "<h3>您输入的信息如下:</h3>";
		echo $text;
	}else{
		echo "<h3>您输入的信息非法。</h3>";
	}
}
function test_input($data) {
	$data = trim($data);
	$data = stripslashes($data);
	$data = htmlspecialchars($data);
    if(preg_match("/[0-9]{6}/",$data)){
		return $data;
	}else{
		return "";
	}   
}
?>
上一篇:5.3、使用PCRE扩展正则表达式函数 下一篇:第6章 操作PHP数组