Php URL, email, and mobile phone number Regular Expression Code-PHP source code

Source: Internet
Author: User
This article introduces in detail several regular expressions that are commonly used in php development, this article describes in detail the regular expressions commonly used in php development, such as the regular expression codes for websites, emails, and mobile phone numbers.

Script ec (2); script

1. Email judgment:

A domain name is composed of a specific character set, English letters, numbers, and "-" (that is, a hyphen or minus sign) of Chinese Characters in different countries, but cannot start or end with "-". "-" cannot appear consecutively. The domain name is case-insensitive. The domain name can contain up to 60 bytes (including the suffix. com,. net, and. org ).
/^ [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:
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:

Function is_email ($ email ){
Return strlen ($ email)> 6 & preg_match ("/^ [w-.] + @ [w-] + (. w +) + $/", $ email );
}
?>

Example 2

The Code is as follows:



$ 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 );
?>

Result

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

2. Determine the Url:

Example 1

The Code is as follows:

Function is_url ($ str ){
Return preg_match ('/^ http: // [A-Za-z0-9] +. [A-Za-z0-9] + [/=? % -&_~ '@ []': +!] * ([^ <> "]) * $/", $ Str );
}


Example 2

Php judges the url address and automatically converts it to a hyperlink. In a string, match the url with a regular expression. After converting the url to a hyperlink, click the accessible url.

The Code is as follows:

Function autolink ($ foo)
{
$ Foo = eregi_replace ('(f | ht) {1} tp: //) [-a-zA-Z0-9 @: % _/+ .~ #? & // =] +) ','/1', $ foo );
If (strpos ($ foo, "http") = FALSE ){
$ Foo = eregi_replace ('(www. [-a-zA-Z0-9 @: % _/+ .~ #? & // =] +) ','/1', $ foo );
} Else {
$ Foo = eregi_replace ('([[: space:] () [{}]) (www. [-a-zA-Z0-9 @: % _/+ .~ #? & // =] +) ','/123', $ foo );
}
Return $ foo;
}

?>


3. Determine the mobile phone number:

Example 1

The Code is as follows:

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 )){
// Verification passed

} Else {
// Incorrect Mobile Phone Number 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.