This example needs to verify the content: E-mail address, URL address, telephone number, ZIP code, verification method for everyone to share for reference, the specific content as follows
1. Verification of e-mail address
<?php/* Verify the mail address */function checkmail ($email) {//username, by "\w" format character, "-" or "." Composition $email_name= "\w| (\w[-.\w]*\w) ";//The first paragraph in the domain name, the rule is similar to the user name, excluding the dot". " $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 paragraph of the domain name 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, the following returns the Success 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, the URL address of the check
<?php/* Check URL address */function checkdomain ($domain) {return Ereg ("^ (HTTP|FTP) s?:/ /(www\.)?. + (com|net|org) $ ", $domain);} $rs = Checkdomain ("www.taodoor.com");//Return False $rs= Checkdomain ("http://www.taodoor.com");//return True?>
3. Telephone number
<?php/* Verify the phone number */function Checktelno ($tel) {//Remove the extra delimiter $tel= ereg_replace ("[\ \ \ \ \ \ \ \). -] "," ", $tel);//contains only numbers, which should be at least one 6-bit telephone number (that is, no area code) if (Ereg (" ^\d+$ ", $tel)) {return true;} Else{return false;}} $rs = Checktelno ("(086)-0411-12345678");//Return to True?>
4, the verification of the ZIP code
<?php/* Check zip code */function checkzipcode ($code) {//remove extra delimiter $code = Preg_replace ("/[\. -]/"," ", $code);//contains a 6-bit postal code if (Preg_match ("/^\d{6}$/", $code)) {return true;} Else{return false;}} $rs = Checkzipcode ("123456");//return True?>
I hope this article will help you learn PHP programming.
The above describes the PHP regular expression verification (e-mail address, URL address, phone number, zip code), including the content, I hope to be interested in PHP tutorial friends helpful.