Write a simple regular Expression tool class

Source: Internet
Author: User

Follow the video, but also personally knocked out of such a regular expression of the class, feel very good, really warm and know the new, originally wanted to put the note to write the high point of a little, today late a bit tired, the first simple to write, have time to perfect perfect.

<?phpclass regextool{//contains some commonly used regular expressions private  $validate =array (' Require '    =>    '/.+/', ' email '      =>   '/^\w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *$/', ' url '        =>   '/^http (s?): \ /\/(?: [A-za-z0-9-]+\.) +[a-za-z]{2,4} (?: [\/\?#][\/=\?%\ -&~ ' @[\]\ ': +!\.#\w]*)? $/', ' Currency '   =>   '/^\d+ (\.\d+)? $/', ' number '      =>   '/^\d+$/', ' Zip '        =>   '/^\d{6 }$/', ' Integer '    =>   '/^[-\+]?\d+$/', ' Double '     =>    '/^[-\+]?\d+ (\.\d+) $/', ' Chinese '    =>   '/^[a-za-z]+$/', ' qq ' = '/^\d{5,11 ' $/', ' mobile ' = '/^1 (3|4|5|7|8) \d{9}$/',);/******************** definition Returns the result of whether the definition matches or is to get a matching result * when false to verify the result, is true  or false   whether to match to *****************/private  $returnMatchResult =false;//definitionFix mode, default to empty private  $fixMode =null;//match result array//Verify matching result if success is true  otherwise   is falseprivate  $isMatch = false;//Constructors   Two parameters   defines the return result type, and the correction mode public function __construct ($returnMatchResult =false,$ Fixmode=null) {$this->returnmatchresult= $returnMatchResult;//assigns a value to the member property $this->fixmode= $fixMode;} /********************************* core Matching method The first parameter of the regular expression, the second is the target data * $pattern   If in the member property $validate, use the ready-made expression directly, Does not exist as he himself is an expression **************************/private function regex ($pattern, $subject) {if (array_key_exists (Strtolower ($pattern),  $this->validate)) {$pattern = $this->validate[$pattern]. $this->fixmode;} Judging return type $this->returnmatchresult?preg_match_all ($pattern,  $subject,  $this->matches): $this Ismatch=preg_match ($pattern,  $subject) ===1;return  $this->getregexresult ();} Depending on getregexresult, returns different results Private function getregexresult () {if ($this->returnmatchresult) {return   $this->matches;} else{return  $this->ismatch;}} //Toggles the returned type, such as when instantiating a Boolean value, and now suddenly wants to match the array public function togglereturntype ($bool =null) {if (empty ($bool)) {$ this->returnmatchresult=! $this->returnmatchresult;} else{$this->returnmatchresult=is_bool ($bool)? $bool:(bool) $bool;}} Toggle Rest mode for the same reason as Public function setfixmode ($fixMode) {$this->fixmode= $fixMode;} Below you can define some common authentication methods//Non-null authentication method Public function noempty ($STR) {return  $this->regex (' Require ', $STR);} Verify Emailpublic function isemail ($str) {return  $this->regex (' email ', $str);} Verify phone number public function ismobile ($str) {return  $this->regex (' mobile ', $STR);} User-defined regular expression Public function check ($pattern, $subject) {return  $this->regex ($pattern, $subject);}} Practical operation, the effect is still rod ^_^ $regex  = new regextool (); $regex  -> setfixmode (' U '); $isEmail =$ Regex -> isemail (' [email protected] '); $isMobile = $regex  -> ismobile (' 56481513 '); Var_dump ($isEmail);echo  '  

After testing, there should be no problem.

Write a simple regular Expression tool 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.