php url, date, ip, email address detection class code

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags .url address check class code date email e-mail

php tutorial url, date, ip, email address detection class code This article provides a really regular expression to detect url address, date format, ip address, and e-mail address is valid verification code.

class validation {
function check_email ($ value) {// check a email address
return (bool) preg_match ('/ ^ [a-z0-9 ._% + -] + @ (?: [a-z0-9 -] +.) + [az] {2,4} $ / i' , $ value);
}

function check_ip ($ value) {// check a ip address
return (bool) preg_match ("/ ^ (?: (?: 25 [0-5] | 2 [0-4] [0-9] | [01]? [0-9] [0-9]?) .) {4} $ / ", $ value.". ");
}

function check_date ($ date, $ format = "dd / mm / yy") {// check a date
if (! preg_match ("/ ([0-9] +) ([./-]) ([0-9] +) (2) ([0-9] +) /", $ date, $ m) ) return false;
$ f = explode ("/", $ format);
$ d [$ f [0]] = $ m [1];
$ d [$ f [1]] = $ m [3];
$ d [$ f [2]] = $ m [5];
return checkdate ($ d ['mm'], $ d ['dd'], $ d ['yyyy']. $ d ['yy']);
}

function check_url ($ url) {// check a url
return (bool) preg_match ("/ ^ (?: (?: ht | f) tp (?: s?): // | ~ / / / :: d +)? (?: (?: / [^ /? #] +) +)? /? (?:? [^?] *)? (#. *)? $ / i " ;
}
}
// call method

$ validation = new validation ();
// test the mailbox
var_dump ($ validation-> check_email ("abc@abc.abc.com")); // true
var_dump ($ validation-> check_email ("abc.abc@abc.com")); // false
// ip detection

var_dump ($ validation-> check_ip ("127.0.0.1.0"));
var_dump ($ validation-> check_ip ("256.0.0.1")); // Date format detection

// true
var_dump ($ validation-> check_date ("2010/1/20", "yyyy / mm / dd"));
var_dump ($ validation-> check_date ("1/20/10", "mm / dd / yy"));
var_dump ($ validation-> check_date ("2010-20-1", "yyyy / dd / mm"));
// false
var_dump ($ validation-> check_date ("2010/2/29", "yyyy / mm / dd"));
var_dump ($ validation-> check_date ("2010/1/20", "yyyy / dd / mm"));
var_dump ($ validation-> check_date ("2010-20 / 1", "yyyy / dd / mm"));
/ *
The use of such methods is very simple, as long as the creation of categories can easily verify the mailbox, date, address, ip is legal Oh.
* /

Related Article

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.