Rumah php教程 PHP源码 php规则验证

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;
}
Salin selepas log masuk

Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn

Alat AI Hot

Undresser.AI Undress

Undresser.AI Undress

Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover

AI Clothes Remover

Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool

Undress AI Tool

Gambar buka pakaian secara percuma

Clothoff.io

Clothoff.io

Penyingkiran pakaian AI

AI Hentai Generator

AI Hentai Generator

Menjana ai hentai secara percuma.

Alat panas

Notepad++7.3.1

Notepad++7.3.1

Editor kod yang mudah digunakan dan percuma

SublimeText3 versi Cina

SublimeText3 versi Cina

Versi Cina, sangat mudah digunakan

Hantar Studio 13.0.1

Hantar Studio 13.0.1

Persekitaran pembangunan bersepadu PHP yang berkuasa

Dreamweaver CS6

Dreamweaver CS6

Alat pembangunan web visual

SublimeText3 versi Mac

SublimeText3 versi Mac

Perisian penyuntingan kod peringkat Tuhan (SublimeText3)