Php Regular Expression verification (email address, Url address, phone number, zip code), regular expression url

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

Php Regular Expression verification (email address, Url address, phone number, zip code), regular expression url

Content to be verified in this example:Email Address, Url address, phone number, zip codeThe verification method is shared with you for your reference. The details are as follows:

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.

Articles you may be interested in:
  • Regular Expressions Used for Form Verification in php
  • PHP Regular Expression verification of Chinese Characters
  • PHP IPV6 Regular Expression Verification Code
  • Php mobile phone number verification Regular Expression
  • Php password Regular Expression for password verification (8-bit length limit)
  • Php URL validation Regular Expression
  • Php user registration information validation Regular Expression
  • Php email address Regular Expression Verification

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.