目錄
回复内容:
首頁 後端開發 php教程 这个PHP验证类如何使用?

这个PHP验证类如何使用?

Jun 06, 2016 pm 08:30 PM
php

比如我写好的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 &gt;= $len) ? true : false;
                break;
            default: //min = 1000 &amp;&amp; $timeArr[0] = 0 &amp;&amp; $dateArr[1] = 0 &amp;&amp; $dateArr[2] = 0 &amp;&amp; $timeArr[0] = 0 &amp;&amp; $timeArr[1] = 0 &amp;&amp; $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 &gt;= $len) ? true : false;
                break;
            default: //min = 1000 &amp;&amp; $timeArr[0] = 0 &amp;&amp; $dateArr[1] = 0 &amp;&amp; $dateArr[2] = 0 &amp;&amp; $timeArr[0] = 0 &amp;&amp; $timeArr[1] = 0 &amp;&amp; $timeArr[2] </code>
登入後複製
登入後複製

<code>include("check.php");
$check = new VerifyAction();
//验证邮箱
$check_email = $check-&gt;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>
登入後複製
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章標籤

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

適用於 Ubuntu 和 Debian 的 PHP 8.4 安裝和升級指南 適用於 Ubuntu 和 Debian 的 PHP 8.4 安裝和升級指南 Dec 24, 2024 pm 04:42 PM

適用於 Ubuntu 和 Debian 的 PHP 8.4 安裝和升級指南

CakePHP 專案配置 CakePHP 專案配置 Sep 10, 2024 pm 05:25 PM

CakePHP 專案配置

CakePHP 日期和時間 CakePHP 日期和時間 Sep 10, 2024 pm 05:27 PM

CakePHP 日期和時間

CakePHP 檔案上傳 CakePHP 檔案上傳 Sep 10, 2024 pm 05:27 PM

CakePHP 檔案上傳

CakePHP 路由 CakePHP 路由 Sep 10, 2024 pm 05:25 PM

CakePHP 路由

討論 CakePHP 討論 CakePHP Sep 10, 2024 pm 05:28 PM

討論 CakePHP

如何設定 Visual Studio Code (VS Code) 進行 PHP 開發 如何設定 Visual Studio Code (VS Code) 進行 PHP 開發 Dec 20, 2024 am 11:31 AM

如何設定 Visual Studio Code (VS Code) 進行 PHP 開發

CakePHP 快速指南 CakePHP 快速指南 Sep 10, 2024 pm 05:27 PM

CakePHP 快速指南

See all articles