PHP Regular Expression validation (email address, URL address, phone number, postal code)

Source: Internet
Author: User
Tags ereg learn php learn php programming php programming php regular expression
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.

  • 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.