PHP Regular Expression validation (mail address, URL address, phone number, postal code) _php tips

Source: Internet
Author: User
Tags ereg php programming php regular expression

This article example needs to verify the content: The mail address, the URL address, the telephone number, the postal code , the verification method share for everybody reference, the concrete content is as follows

1, the verification of the e-mail address

<?php/
* Verify email address/
function Checkmail ($email) {
//user name, by "\w" format character, "-" or "." Composed
$email _name= "\w| ( \w[-.\w]*\w) ";
The first paragraph in the domain name is similar to the rule and user name, excluding the dot number "."
$code _at= "@";
$per _domain= "\w| (\w[-\w]*\w) ";
The middle portion of the domain name, up to two paragraphs
$mid _domain= "(\.") $per _domain. ") {0,2}";
The last paragraph in 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 are returned to the successful
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
/* Verify 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 phone number/
function Checktelno ($tel)
{
//Remove superfluous separator
$tel = ereg_replace ("[\ \] \. -] "," ", $tel);
Contains only numbers, 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 True
?>

4. Check the ZIP code

<?php
/* Verify ZIP Code
/function Checkzipcode ($code)
{
//Remove superfluous 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");/return True
?>

Hopefully this article will help you learn about PHP programming.

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.