比如我写好的POST提交例子,验证邮箱和值是否未空?
<code>include("check.php"); $check = new VerifyAction(); if(?){ echo '提交成功!'; }else{ echo '填写不对 重填写'; } <form action="index.php" method="post"> <input type="text" name="name"><br> <input type="text" name="email"><br> <input type="submit"> </form> </code>
验证类 check.php
<code><?php /** * 验证类 * * @lastmodify 2014-5-16 * @author jy625 */ class VerifyAction{ /** * 是否为空值 */ public static function isEmpty($str){ $str = trim($str); return !empty($str) ? true : false; } /** * 数字验证 * param:$flag : int是否是整数,float是否是浮点型 */ public static function isNum($str,$flag = 'float'){ if(!self::isEmpty($str)) return false; if(strtolower($flag) == 'int'){ return ((string)(int)$str === (string)$str) ? true : false; }else{ return ((string)(float)$str === (string)$str) ? true : false; } } /** * 名称匹配,如用户名,目录名等 * @param:string $str 要匹配的字符串 * @param:$chinese 是否支持中文,默认支持,如果是匹配文件名,建议关闭此项(false) * @param:$charset 编码(默认utf-8,支持gb2312) */ public static function isName($str,$chinese = true,$charset = 'utf-8'){ if(!self::isEmpty($str)) return false; if($chinese){ $match = (strtolower($charset) == 'gb2312') ? "/^[".chr(0xa1)."-".chr(0xff)."A-Za-z0-9_-]+$/" : "/^[x{4e00}-x{9fa5}A-Za-z0-9_]+$/u"; }else{ $match = '/^[A-za-z0-9_-]+$/'; } return preg_match($match,$str) ? true : false; } /** * 邮箱验证 */ public static function isEmail($str){ if(!self::isEmpty($str)) return false; return preg_match("/([a-z0-9]*[-_\.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z]{2,3}([\.][a-z]{2})?/i",$str) ? true : false; } //手机号码验证 public static function isMobile($str){ $exp = "/^13[0-9]{1}[0-9]{8}$|15[012356789]{1}[0-9]{8}$|18[012356789]{1}[0-9]{8}$|14[57]{1}[0-9]$/"; if(preg_match($exp,$str)){ return true; }else{ return false; } } /** * URL验证,纯网址格式,不支持IP验证 */ public static function isUrl($str){ if(!self::isEmpty($str)) return false; return preg_match('#(http|https|ftp|ftps)://([w-]+.)+[w-]+(/[w-./?%&=]*)?#i',$str) ? true : false; } /** * 验证中文 * @param:string $str 要匹配的字符串 * @param:$charset 编码(默认utf-8,支持gb2312) */ public static function isChinese($str,$charset = 'utf-8'){ if(!self::isEmpty($str)) return false; $match = (strtolower($charset) == 'gb2312') ? "/^[".chr(0xa1)."-".chr(0xff)."]+$/" : "/^[x{4e00}-x{9fa5}]+$/u"; return preg_match($match,$str) ? true : false; } /** * UTF-8验证 */ public static function isUtf8($str){ if(!self::isEmpty($str)) return false; return (preg_match("/^([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}/",$word) == true || preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}$/",$word) == true || preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){2,}/",$word) == true) ? true : false; } /** * 验证长度 * @param: string $str * @param: int $type(方式,默认min <= $str <= max) * @param: int $min,最小值;$max,最大值; * @param: string $charset 字符 */ public static function length($str,$type=3,$min=0,$max=0,$charset = 'utf-8'){ if(!self::isEmpty($str)) return false; $len = mb_strlen($str,$charset); switch($type){ case 1: //只匹配最小值 return ($len >= $min) ? true : false; break; case 2: //只匹配最大值 return ($max >= $len) ? true : false; break; default: //min = 1000 && $timeArr[0] = 0 && $dateArr[1] = 0 && $dateArr[2] = 0 && $timeArr[0] = 0 && $timeArr[1] = 0 && $timeArr[2] </code>
回复内容:
比如我写好的POST提交例子,验证邮箱和值是否未空?
<code>include("check.php"); $check = new VerifyAction(); if(?){ echo '提交成功!'; }else{ echo '填写不对 重填写'; } <form action="index.php" method="post"> <input type="text" name="name"><br> <input type="text" name="email"><br> <input type="submit"> </form> </code>
验证类 check.php
<code><?php /** * 验证类 * * @lastmodify 2014-5-16 * @author jy625 */ class VerifyAction{ /** * 是否为空值 */ public static function isEmpty($str){ $str = trim($str); return !empty($str) ? true : false; } /** * 数字验证 * param:$flag : int是否是整数,float是否是浮点型 */ public static function isNum($str,$flag = 'float'){ if(!self::isEmpty($str)) return false; if(strtolower($flag) == 'int'){ return ((string)(int)$str === (string)$str) ? true : false; }else{ return ((string)(float)$str === (string)$str) ? true : false; } } /** * 名称匹配,如用户名,目录名等 * @param:string $str 要匹配的字符串 * @param:$chinese 是否支持中文,默认支持,如果是匹配文件名,建议关闭此项(false) * @param:$charset 编码(默认utf-8,支持gb2312) */ public static function isName($str,$chinese = true,$charset = 'utf-8'){ if(!self::isEmpty($str)) return false; if($chinese){ $match = (strtolower($charset) == 'gb2312') ? "/^[".chr(0xa1)."-".chr(0xff)."A-Za-z0-9_-]+$/" : "/^[x{4e00}-x{9fa5}A-Za-z0-9_]+$/u"; }else{ $match = '/^[A-za-z0-9_-]+$/'; } return preg_match($match,$str) ? true : false; } /** * 邮箱验证 */ public static function isEmail($str){ if(!self::isEmpty($str)) return false; return preg_match("/([a-z0-9]*[-_\.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z]{2,3}([\.][a-z]{2})?/i",$str) ? true : false; } //手机号码验证 public static function isMobile($str){ $exp = "/^13[0-9]{1}[0-9]{8}$|15[012356789]{1}[0-9]{8}$|18[012356789]{1}[0-9]{8}$|14[57]{1}[0-9]$/"; if(preg_match($exp,$str)){ return true; }else{ return false; } } /** * URL验证,纯网址格式,不支持IP验证 */ public static function isUrl($str){ if(!self::isEmpty($str)) return false; return preg_match('#(http|https|ftp|ftps)://([w-]+.)+[w-]+(/[w-./?%&=]*)?#i',$str) ? true : false; } /** * 验证中文 * @param:string $str 要匹配的字符串 * @param:$charset 编码(默认utf-8,支持gb2312) */ public static function isChinese($str,$charset = 'utf-8'){ if(!self::isEmpty($str)) return false; $match = (strtolower($charset) == 'gb2312') ? "/^[".chr(0xa1)."-".chr(0xff)."]+$/" : "/^[x{4e00}-x{9fa5}]+$/u"; return preg_match($match,$str) ? true : false; } /** * UTF-8验证 */ public static function isUtf8($str){ if(!self::isEmpty($str)) return false; return (preg_match("/^([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}/",$word) == true || preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}$/",$word) == true || preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){2,}/",$word) == true) ? true : false; } /** * 验证长度 * @param: string $str * @param: int $type(方式,默认min <= $str <= max) * @param: int $min,最小值;$max,最大值; * @param: string $charset 字符 */ public static function length($str,$type=3,$min=0,$max=0,$charset = 'utf-8'){ if(!self::isEmpty($str)) return false; $len = mb_strlen($str,$charset); switch($type){ case 1: //只匹配最小值 return ($len >= $min) ? true : false; break; case 2: //只匹配最大值 return ($max >= $len) ? true : false; break; default: //min = 1000 && $timeArr[0] = 0 && $dateArr[1] = 0 && $dateArr[2] = 0 && $timeArr[0] = 0 && $timeArr[1] = 0 && $timeArr[2] </code>
<code>include("check.php"); $check = new VerifyAction(); //验证邮箱 $check_email = $check->isEmail($_POST['email']); if($check_email) { //是正确的邮箱 }else{ //邮箱填写有问题... } </code>
不知道你明白了么?如果不明白 加群讨论下告诉你 群:231566327
<code>include("check.php"); if($_SERVER['REQUEST_METHOD']=='POST'){ if(!VerifyAction::isEmpty($_POST['name'])){ die("用户名不能为空") ; }elseif(!VerifyAction::isEmail($_POST['email'])){ die( "无效邮箱账号"); } echo "验证通过"; } </code>

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools
