Common regular expressions are used to verify information, such as the mobile phone number of the website email address.

Source: Internet
Author: User
Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn common regular expressions to verify information, such as the website email phone number.
/**
* Common regular expressions are used to verify information, such as the mobile phone number of the website email address.
*/

Class check {

/**
* Regular Expressions verify the email format
*
* @ Param string $ str the email address to be verified
* @ Return boolean
*/
Public static function isEmail ($ str ){

If (! $ Str ){
Return false;
}

Return preg_match (# [a-z0-9 & \-_.] + @ [\ w \-_] + ([\ w \-.] + )? \. [\ W \-] + # is ', $ str )? True: false;
}

/**
* Regular Expression verification URL
*
* @ Param string $ str URL to be verified
* @ Return boolean
*/
Public static function isUrl ($ str ){

If (! $ Str ){
Return false;
}

Return preg_match ('# (http | https | ftp | ftps): // ([\ w-] + \.) + [\ w-] + (/[\ w -. /? % & =] *)? # I ', $ str )? True: false;
}

/**
* Verify that the string contains Chinese Characters
*
* @ Param integer $ string the string to be verified. Note: String encoding only supports UTF-8
* @ Return boolean
*/
Public static function isChineseCharacter ($ string ){

If (! $ String ){
Return false;
}

Return preg_match ('~ [\ X {4e00}-\ x {9fa5}] ++ ~ U', $ string )? True: false;
}

/**
* Verify that the string contains invalid characters.
*
* @ Param string $ string the string to be verified
* @ Return boolean
*/
Public static function isInvalidStr ($ string ){

If (! $ String ){
Return false;
}

Return preg_match ('#[! # $ % ^ &*(){}~ "'\';:? + = <>/\ [\] + # ', $ String )? True: false;
}

/**
* Use a regular expression to verify the postal code
*
* @ Param integer $ num the zip code to be verified
* @ Return boolean
*/
Public static function isPostNum ($ num ){

If (! $ Num ){
Return false;
}

Return preg_match ('# ^ [1-9] [0-9] {5 }$ #', $ num )? True: false;
}

/**
* Regular Expression to verify the ID card number
*
* @ Param integer $ num ID card number to be verified
* @ Return boolean
*/
Public static function isPersonalCard ($ num ){

If (! $ Num ){
Return false;
}

Return preg_match ('# ^ [\ d] {15 }$ | ^ [\ d] {18 }$ #', $ num )? True: false;
}

/**
* Use regular expressions to verify IP addresses. Note: Only IPv4 addresses are allowed.
*
* @ Param string $ str IP address to be verified
* @ Return boolean
*/
Public static function isIp ($ str ){

If (! $ Str ){
Return false;
}

If (! Preg_match ('# ^ \ d {1, 3} \. \ d {1, 3} \. \ d {1, 3} \. \ d {1, 3} $ #', $ str )){
Return false;
}

$ IpArray = explode ('.', $ str );

// The real IP address cannot exceed 255 (0-255)
Return ($ ipArray [0] <= 255 & $ ipArray [1] <= 255 & $ ipArray [2] <= 255 & $ ipArray [3] <= 255 )? True: false;
}

/**
* Use a regular expression to verify the ISBN number of a publication
*
* @ Param integer $ str the ISBN number to be verified, usually composed of 13 digits
* @ Return boolean
*/
Public static function isBookIsbn ($ str ){

If (! $ Str ){
Return false;
}

Return preg_match ('# ^ 978 [\ d] {10 }$ | ^ 978-[\ d] {10 }# #', $ str )? True: false;
}

/**
* Use a regular expression to verify the mobile phone number (Mainland China)
* @ Param integer $ num the phone number to be verified
* @ Return boolean
*/
Public static function isMobile ($ num ){

If (! $ Num ){
Return false;
}

Return preg_match ('# ^ 13 [\ d] {9} $ | 14 ^ [0-9] \ d {8} | ^ 15 [0-9] \ d {8} $ | ^ 18 [0-9] \ d {8} $ #', $ num )? True: false;
}

/**
* Check whether the string is null.
*
* @ Access public
* @ Param string $ string content
* @ Return boolean
*/
Public static function isMust ($ string = null ){

// Parameter Analysis
If (is_null ($ string )){
Return false;
}

Return empty ($ string )? False: true;
}

/**
* Check the string length.
*
* @ Access public
* @ Param string $ string content
* @ Param integer $ min minimum number of strings
* @ Param integer $ max maximum number of strings
*/
Public static function isLength ($ string = null, $ min = 0, $ max = 255 ){

// Parameter Analysis
If (is_null ($ string )){
Return false;
}
// Obtain the string length
$ Length = strlen (trim ($ string ));

Return ($ length >=( int) $ min) & ($ length <= (int) $ max ))? True: false;
}
}

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

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.