PHP Regular Expression verification-PHP source code

Source: Internet
Author: User
Tags php regular expression
Ec (2); & nbsp; & lt ;? Php ** & nbsp; * PHP Regular Expression verification Class & nbsp; * CodebyT. t.R & nbsp; * [url] www.Gx3.cn [url] [url] Gx3.cn [url] & nbsp; * QQ: 252319874 & nbsp; * classregExp {& nbsp; & nbsp; stat script ec (2); script




/**
* PHP Regular Expression verification class
* Code by T.T. R
* [Url] http://www.Gx3.cn [/url] [url] http://Gx3.cn [/url]
* QQ: 252319874
*/
Class regExp
{


Static function strTrim ($ str)
{
Return preg_replace ("/s/", "", $ str );
}



Static function userName ($ str, $ type, $ len)
{
$ Str = self: strTrim ($ str );
If ($ len {
Return false;
} Else {
Switch ($ type)
{
Case "EN": // English only
If (preg_match ("/^ [a-zA-Z] + $/", $ str ))
{
Return true;
} Else {
Return false;
}
Break;
Case "ENNUM": // english number
If (preg_match ("/^ [a-zA-Z0-9] + $/", $ str ))
{
Return true;
} Else {
Return false;
}
Break;
Case "ALL": // allowed symbols (|-_ letter/number)
If (preg_match ("/^ [|-_ a-zA-Z0-9] + $/", $ str ))
{
Return true;
} Else {
Return false;
}
Break;
}
}
}



Static function passWord ($ min, $ max, $ str)
{
$ Str = self: strTrim ($ str );
If (strlen ($ str) >=$ min & strlen ($ str) <= $ max)
{
Return true;
} Else {
Return false;
}
}


Static function Email ($ str)
{
$ Str = self: strTrim ($ str );

If (preg_match ("/^ ([a-z0-9 _] | \-| \.) + @ ([a-z0-9 _] | \-) + \.) {1, 2} [a-z] {2, 4} $/I ", $ str ))
{
Return true;
} Else {
Return false;
}

}


Static function idCard ($ str)
{
$ Str = self: strTrim ($ str );
If (preg_match ("/^ ([0-9] {15} | [0-9] {17} [0-9a-z]) $/I", $ str ))
{
Return true;
} Else {
Return false;
}
}




Static function Phone ($ type, $ str)
{
$ Str = self: strTrim ($ str );
Switch ($ type)
{
Case "CHN ":
If (preg_match ("/^ ([0-9] {3} | 0 [0-9] {3})-[0-9] {7, 8} $ /", $ str ))
{
Return true;
} Else {
Return false;
}
Break;
Case "INT ":
If (preg_match ("/^ [0-9] {4}-([0-9] {3} | 0 [0-9] {3 }) -[0-9] {7,8 }$/", $ str ))
{
Return true;
} Else {
Return false;
}
Break;
}
}
}
$ Str = "008-010-2711204 ";
If (regExp: Phone ("INT", $ str ))
{
Echo "OK ";
} Else {
Echo "no ";
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.