Php url, email, mobile phone number of the regular expression code

Source: Internet
Author: User
Tags regular expression

1. Judge Email:

Domain names are composed of specific character sets, English letters, numbers, and "-" (that is, hyphens or minus signs) of each country's text, but neither the beginning nor the end can contain "-", "-" and "-" cannot appear continuously. The letters in the domain name are not case-insensitive. The domain name can be up to 60 bytes (including suffix. com,. NET,. org, and so on).
/^[a-z] ([a-z0-9]*[-_]?[ a-z0-9]+) *@ ([a-z0-9]*[-_]?[ a-z0-9]+) +[.] [A-z] {2,3} ([.] [A-z] {2})? $/i;
/content/I forms a case-insensitive regular expression;

The basic rules are as follows

The code is as follows Copy Code
Preg_match ('/^[a-z0-9_-]+. [ _a-z0-9-]+) *@ ([_a-z0-9-]+.) + ([a-z]{2}
|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel) $/', $email)

Example 1

The code is as follows Copy Code

<?php
function Is_email ($email) {
return strlen ($email) > 6 && preg_match ("/^[w-.] +@[w-]+ (. w+) +$/", $email);
}
?>

Example 2

The code is as follows Copy Code

<body>
<?php
$email _pattern = '/w{6,16}@w{1,}.w{2,3}/i ';
$email _valid = ' test_123@126.net ';
$email _invalid = ' test@test%@111@com ';
$matches = Array ();

Preg_match ($email _pattern, $email _valid, $matches []);
Preg_match ($email _pattern, $email _invalid, $matches []);

Var_dump ($matches);
?>
</body>

Results

Array (2) {[0]=> Array (1) {[0]=> string () "Test_123@126.net"} [1]=> Array (0) {}}

2. Determine the URL:

Example 1

The code is as follows Copy Code

function Is_url ($STR) {
Return Preg_match ("/^http://[a-za-z0-9]+.[ a-za-z0-9]+[/=?%-&_~ ' @[] ': +!] * ([^<> "]) *$/", $str);
}


Example 2

PHP to determine the URL address and automatically converted to hyperlinks, in a string of regular expressions to match the URL, in the conversion URL to a hyperlink, click the accessible address

  code is as follows copy code

<?php
function Autolink ($foo)
{
$foo = Eregi_replace (((F|HT) { 1}tp://) [-a-za-z0-9@:%_/+.~#?&//=]+] ', ' <a href= '/1 "mce_href="/1 "Target=_blank rel=nofollow>/1</a > ', $foo);
if (Strpos ($foo, "http") = = FALSE) {
$foo = eregi_replace (' (www.[ -a-za-z0-9@:%_/+.~#?&//=]+) ', ' <a href= "HTTP:///1" mce_href= "HTTP:///1" Target=_blank rel=nofollow >/1< /a> ', $foo);
}else{
$foo = Eregi_replace ([[: Space:] () [{}]) (www.[ -a-za-z0-9@:%_/+.~#?&//=]+) ', '/1<a href= "HTTP:///2" mce_href= "HTTP:///2" Target=_blank Rel=nofollow </a> ', $foo);
}
Return $foo;
}

?


3. Judge Mobile phone Number:

Example 1

The code is as follows Copy Code

function Is_mobile ($STR) {
Return Preg_match (/^ (d{3}) | ( d{3}-))? 13d{9}$/", $STR);
}

Example 2

if (Preg_match ("/^13[0-9]{1}[0-9]{8}$|15[0189]{1}[0-9]{8}$|189[0-9]{8}$/", $mobilephone)) {
Validation through

}else{
Phone number is not in the right format

}

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.