php规则验证

May 22, 2016 pm 06:24 PM

php代码

function check_data($string, $type='email'){
    $return = FALSE;
    switch($type){
        case 'email'        : {$return = preg_match("/^(\w+[-+.]*\w+)*@(\w+([-.]*\w+)*\.\w+([-.]*\w+)*)$/", $string); break;}
        case &#39;http&#39;         : {$return = preg_match("/^http:\/\/[A-Za-z0-9-]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\&#39;:+!]*([^<>\"])*$/", $string); break;}
        case &#39;qq&#39;           : {$return = preg_match("/^[1-9]\d{4,11}$/", $string); break;}
        case &#39;post&#39;         : {$return = preg_match("/^[1-9]\d{5}$/", $string); break;}
        case &#39;idnum&#39;        : {$return = preg_match("/^\d{15}(\d{2}[A-Za-z0-9])?$/", $string); break;}
        case &#39;china&#39;        : {$return = preg_match("/^[".chr(0xa1)."-".chr(0xff)."]+$/", $string); break;} //GBK中文
        case &#39;english&#39;      : {$return = preg_match("/^[A-Za-z]+$/", $string); break;}
        case &#39;mobile&#39;       : {$return = preg_match("/^((\(\d{3}\))|(\d{3}\-))?((13)|(14)|(15)|(17)|(18)){1}\d{9}$/", $string); break;}
        case &#39;phone&#39;        : {$return = preg_match("/^((\(\d{3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}$/", $string); break;}
        case &#39;safe&#39;         : {$return = preg_match("/^(([A-Z]*|[a-z]*|\d*|[-_\~!@#\$%\^&\*\.\(\)\[\]\{\}<>\?\\\/\&#39;\"]*)|.{0,5})$|\s/", $string) != 0 ? TRUE : FALSE; break;}
        case &#39;age&#39;          : {$return = (preg_match("/^(-{0,1}|\+{0,1})[0-9]+(\.{0,1}[0-9]+)$/", $string) && intval($string) = 12) ? TRUE : FALSE; break;}
        case &#39;eng_num&#39;      : {$return = preg_match("/^[A-Za-z0-9]+$/", $string); break;}
        case &#39;password&#39;     : {$return = (preg_match("/^[A-Za-z0-9]+$/", $string) && strlen($string) = 6) ? TRUE : FALSE; break;}
        case &#39;datetime&#39;     : {$return = preg_match(&#39;/^[\d]{4}-[\d]{1,2}-[\d]{1,2}\s[\d]{1,2}:[\d]{1,2}:[\d]{1,2}$/&#39;, $string); break;}
        case &#39;datetimes&#39;    : {$return = preg_match(&#39;/^[\d]{4}-[\d]{2}-[\d]{2}\s[\d]{2}:[\d]{2}:[\d]{2}$/&#39;, $string); break;}
        case &#39;date&#39;         : {$return = preg_match(&#39;/^[\d]{4}-[\d]{1,2}-[\d]{1,2}$/&#39;, $string); break;}
        case &#39;dates&#39;        : {$return = preg_match(&#39;/^[\d]{4}-[\d]{2}-[\d]{2}$/&#39;, $string); break;}
        case &#39;time&#39;         : {$return = preg_match(&#39;/^[\d]{1,2}:[\d]{1,2}:[\d]{1,2}$/&#39;, $string); break;}
        case &#39;times&#39;        : {$return = preg_match(&#39;/^[\d]{2}:[\d]{2}:[\d]{2}$/&#39;, $string); break;}
        case &#39;ip&#39;           : {$return = preg_match("/^\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b$/", $string); break;}
        case &#39;incchinese&#39;   : {$return = preg_match(&#39;/[\x{4e00}-\x{9fa5}]+/u&#39;, $string); break;} //是否包含中文
        case &#39;plusnum&#39;      : {$return = preg_match(&#39;/^[1-9]*[1-9][0-9]*$/&#39;, $string); break;} //是否是正整数
        case &#39;hostrecord&#39;   : {$return = preg_match(&#39;/^[A-Z_a-z0-9][A-Za-z0-9-]+(\.[A-Za-z0-9-_]+)*$/&#39;, $string); break;} //正确的主机记录,english
        case &#39;cnhostrecord&#39; : {$return = preg_match(&#39;/^[_a-zA-Z0-9]*([\x{4e00}-\x{9fa5}]*[-a-zA-Z0-9\.]*)+[a-zA-Z0-9_]$/iu&#39;, $string); break;} //正确的主机记录,english chinese
        case &#39;domain&#39;       : {$return = preg_match(&#39;/^[A-Za-z0-9][A-Za-z0-9-]+(\.[A-Za-z0-9-]+){1,3}$/&#39;, $string); break;} //是否是域名
        case &#39;cndomain&#39;     : {$return = (preg_match(&#39;/[\x{4e00}-\x{9fa5}]+/u&#39;, $string) && preg_match(&#39;/^([-a-zA-Z0-9\.]*[\x{4e00}-\x{9fa5}]*[-a-zA-Z0-9\.]*)+\.(中国|公司|网络|CN|COM|NET)$/iu&#39;, $string)) ? TRUE : FALSE; break;}  //是否中文域名
        case &#39;mac&#39;          : {$return = preg_match(&#39;/^[a-fA-F\d]{2}:[a-fA-F\d]{2}:[a-fA-F\d]{2}:[a-fA-F\d]{2}:[a-fA-F\d]{2}:[a-fA-F\d]{2}$/&#39;, $string); break;}
        case &#39;ipv6&#39;         : {$return = preg_match(&#39;/^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/&#39;, $string); break;}
    }
    gettype($return) == &#39;integer&#39; && $return = $return == 0 ? FALSE : TRUE;
    return $return;
}
ログイン後にコピー

このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)