A generic PHP regular expression that matches or detects or extracts a particular character class

Source: Internet
Author: User
Tags email string php regular expression

In PHP development, daily indispensable to use regular expression, can be re-written every time, sometimes forget a function also to check the manual, so, the time to write a regular expression on the daily use of the zone class, easy to move the call. (^_^ a little lazy).

/*/////////////////////////////////////////////////////////////* Class Matchall function: Mainly used for PHP daily use of several detection matching or extracting specific characters./* Main parameter setting Description: $target _str as the target character or character or number to be detected.  * $isFind true: extracts the match character; false: detects only the existence of a specific character.           * $isTotal true: Exact match mode; false: Partial match mode. * @author: Purpen//* @email: [email protected]//* @data: 2007-07-29//*/////////////////////////////////////////////////////   classMatchAll {/** Function: Used to detect whether the IP address is legitimate or to extract IP characters from the string.       * * @param string $target _str * @param boolean $isFind * @param boolean $isTotal * @return Mixed */       Public Static functionIp_match ($target _str,$isFind=false,$isTotal=false) {           //pattern of matches taken        if($isTotal==true) {               $pattern= "/^ (\d{1,3}\.) {3}\d{1,3}$/"; }Else{               $pattern= "/(\d{1,3}\.) {3}\d{1,3}/"; }           returnSelf::get_match_result ($target _str,$pattern,$isFind); }       /** Function: Used to detect whether the ID number is legal or to extract the social Security number characters from the string.       * * @param mixed $target _str * @param boolean $isFind * @param boolean $isTotal * @return Mixed */       Public Static functionIdcard_match ($target _str,$isFind=false,$isTotal=false){           //pattern of matches taken        if($isTotal==true){               $pattern= "/^ (\d{18}|\d{17}x|\d{15}) $/"; }Else{               $pattern= "/(\d{18}|\d{17}x|\d{15})/"; }           returnSelf::get_match_result ($target _str,$pattern,$isFind); }       /** Function: Used to detect whether a phone number is legal or to extract a phone number from a long string of characters. * @param mixed $target _str * (for example: 010-87965421, (010) 87965421, 87965421, 0394-5568447, (0394) 5568447, 5568447) * @p Aram Boolean $isFind * @param boolean $isTotal * @return mixed*/       Public Static functionPhone_match ($target _str,$isFind=false,$isTotal=false) {           //using matching mode        if($isTotal==true) {               $pattern= "/^ ((? \d{3,4}\))? | (\d{3,4}-)?) \d{7,8}$/"; }Else{               $pattern= "/((\ (? \d{3,4}\))? | (\d{3,4}-)?) \d{7,8}/"; }           returnSelf::get_match_result ($target _str,$pattern,$isFind); }        /** Function: Used to detect whether the postal code is legal or extract the postal code characters from the character long string.       * * @param mixed $target _str * @param boolean $isFind * @param boolean $isTotal * @return Mixed */       Public Static functionPostalcode_match ($target _str,$isFind=false,$isTotal=false) {           //pattern of matches taken        if($isTotal==true) {               $pattern= "/^\d{6}$/"; }Else{               $pattern= "/\d{6}/"; }              returnSelf::get_match_result ($target _str,$pattern,$isFind); }       /** Function: Used for exact matching, troop matching or the method of presenting the network URL. * @param string $target _str * @param boolean $isFind whether to extract the matching target * @param whether the Boolean $isTotal match exactly * @retur N Mixed*/       Public Static functionNeturl_match ($target _str,$isFind=false,$isTotal=false) {           //the matching pattern used        if($isTotal==true) {               $pattern= "/^ (http\:\/\/)?" [A-za-z0-9]+ (\.[ a-za-z0-9]+) *.+$/"; }Else{               $pattern= "/(http\:\/\/)? [A-za-z0-9]+ (\.[ a-za-z0-9]+) */"; }           returnSelf::get_match_result ($target _str,$pattern,$isFind); }       /** Function: Used to detect whether an email is legitimate or to extract an email string from a string. * @param string $target _str * @param boolean $isFind whether to extract the string * @param whether the Boolean $isTotal matches exactly * @return Mixed*/       Public Static functionEmail_match ($target _str,$isFind=false,$isTotal=false){           //pattern of matches taken        if($isTotal==true){               $pattern= "/^[_a-za-z0-9-]+ (\.[ _a-za-z0-9-]+) *@[a-za-z0-9_-]+ (\.[ a-za-z0-9_-]+) *$/"; }Else{               $pattern= "/[_a-za-z0-9-]+ (\.[ _a-za-z0-9-]+) *@[a-za-z0-9_-]+ (\.[ a-za-z0-9_-]+) */"; }           returnSelf::get_match_result ($target _str,$pattern,$isFind); }       /** Function: Used to match, partially match or extract the number of coins. * @param mixed $target _str * @param boolean $isFind * @param boolean $isTotal * @return mixed */       Public Static functionMoney_match ($target _str,$isFind=false,$isTotal=false) {           //with an exact match        if($isTotal==true) {               $pattern= "/^[0-9]{1,3} (, [0-9]{3}) * (\.[ 0-9]+)? $/"; }Else{//partial Match            $pattern= "/[0-9]{1,3} (, [0-9]{3}) * (\.[ 0-9]+)?/"; }           returnSelf::get_match_result ($target _str,$pattern,$isFind); }       /** Function: start to match the target string action.       * * @param mixed $target _str * @param string $pattern * @param boolean $isFind * @return Mixed */       Public functionGet_match_result ($target _str,$pattern,$isFind) {           //whether to extract a matching target        if($isFind==true) {               Preg_match($pattern,$target _str,$result); }Else{               $result=Preg_match($pattern,$target _str); }           return $result; }       /** function: for matching test * @param str $target _str//$target _str = "Select a, B, C, d from D"; */       Public functionAll_match ($target _str) {           $pattern= "/select (. +) from (\s[a-za-z0-9])/I"; Preg_match($pattern,$target _str,$result); $fields=Split(",",$result[1]); Print $result[2]; Print""; return $fields; }   }

A generic PHP regular expression that matches or detects or extracts a particular character class

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.