Php URL, date, IP, email address detection class Code

Source: Internet
Author: User
The code is as follows Copy Code
Class validation{
function Check_email ($value) {//check a email address
return (BOOL) Preg_match ('/^[a-z0-9._%+-]+@: [a-z0-9-]+.) +[a-z]{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?):/ /|~/|/)? (:(?: w+.) +) w+ (?::d +)? (?:(?:/ [^/?#]+)+)?/? (?:? [^?] *)? (#.*) $/i ", $url);
}
}

Call method

The code is as follows Copy Code
$validation = new validation ();
Detecting mailboxes
Var_dump ($validation->check_email ("abc@abc.abc.com"));//true
Var_dump ($validation->check_email ("abc.abc@abc.com"));//false

IP detection

The code is as follows Copy Code
Var_dump ($validation->check_ip ("127.0.0.1.0"));
Var_dump ($validation->check_ip ("256.0.0.1"));

Date format detection

The code is as follows Copy Code

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 a method is very simple, as long as the creation of classes can easily verify the mailbox, date, address, IP is legitimate oh.
*/

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.