: This article describes php regular expression verification (email address, Url address, phone number, and zip code). If you are interested in PHP tutorials, refer to it. The content to be verified in this example is as follows: email address, Url address, phone number, and zip code. the verification method is for your reference.
1. Email Address Verification
<? Php/* verify email address */function checkMail ($ email) {// user name, in the format of "\ w", "-", or ". $ email_name = "\ w | (\ w [-. \ w] * \ w) "; // The first section of the domain name. The rule is similar to the user name, excluding the". "$ code_at =" @ "; $ per_domain =" \ w | (\ w [-\ w] * \ w) "; // The middle part of the domain name, up to two segments $ mid_domain = "(\. ". $ per_domain. ") {0, 2}"; // The last segment of the domain name, which can only be ". com ",". org "or". net "$ end_domain = "(\. (com | net | org) "; $ rs = preg_match ("/^ {$ email_name }@{$ per_domain }{$ mid_domain} {$ end_domain} $ /", $ email); return (bool) $ rs;} // test, under Var_dump (checkMail ("root @ localhost"); var_dump (checkMail ("Frank.Roulan@esun.edu.org"); var_dump (checkMail ("Tom.024-1234@x-power_1980.mail-address.com");?>
2. URL address verification
<? Php/* verify the URL address */function checkDomain ($ domain) {return ereg ("^ (http | ftp) s? : // (Www \.)?. + (Com | net | org) $ ", $ domain) ;}$ rs = checkDomain (" www.taodoor.com "); // returns false $ rs = checkDomain (" http://www.taodoor.com "); // returns true?>
3. phone number
<? Php/* verify the phone number */function checkTelno ($ tel) {// remove the extra separator $ tel = ereg_replace ("[\(\)\. -] "," ", $ tel); // only contains numbers. it must be at least a six-digit phone number (that is, no area code) if (ereg ("^ \ d + $", $ tel) {return true ;}else {return false ;}$ rs = checkTelno ("(086) -0411-12345678 "); // returns true?>
4. postal code verification
<? Php/* verify zip code */function checkZipcode ($ code) {// remove the extra separator $ code = preg_replace ("/[\. -]/"," ", $ code); // contains a 6-bit zip code if (preg_match ("/^ \ d {6} $ /", $ code) {return true;} else {return false; }}$ rs = checkZipCode ("123456"); // returns true?>
I hope this article will help you learn php programming.
The above describes php regular expression verification (email address, Url address, phone number, zip code), including content, hope to be helpful to PHP tutorials.