PHP regular expression matching string URL address and domain name code

Source: Internet
Author: User
Tags php regular expression regular expression to domain

The following format requirements apply to domain names:

1. The labels in a domain name are composed of English letters and numbers. Each label cannot exceed 63 characters and is not case sensitive. You cannot use other punctuation marks except hyphens.

2. Domain names at all levels are connected by real-time (.). The length of a third-level domain name cannot exceed 20 characters.

3. A complete domain name composed of multiple labels cannot exceed 255 characters.

 
Therefore, the regular expression that matches the domain name must be

1, composed of English numbers and "_" [-a-z0-9]

2. Connect each level "."

3. Level 3 domain names cannot exceed 20}

 
The test format is as follows:

The code is as follows: Copy code
<? Php
$ Url = 'www .111cn.net ';
$ Search = '/--- regular N ---/';
If (preg_match ($ search, $ url )){
Echo 'matched ';
} Else {
Echo 'mismatched ';
}
?>

 

I will summarize several common online

Regular 1

B ([a-z0-9] + (-[a-z0-9] +) *.) + [a-z] {2,} B

Error match: length> 60

 

Regular 2

The code is as follows: Copy code

^ ([^-] [A-z0-9A-Z-_] + .) *) [^-] [a-z0-9A-Z-_] + (. [a-zA-Z] {2, 4}) {1, 2} $

Error match: length> 60

Error match: www. te_st.com

 

Regular 3

The code is as follows: Copy code

([Wd-_] + .):? [^-_]) + W {2, 4}

Mismatch: www.111cn.net.cn

Error match: www. te_st.com

 

Regular 4

The code is as follows: Copy code

[A-zA-Z0-9] [-a-zA-Z0-9] {} (. [a-zA-Z0-9] [-a-zA-Z0-9] {}) + .?

No errors

 

Regular 5

The code is as follows: Copy code

(? <=.) ([A-zA-Z0-9] ([a-zA-Z0-9-] {} [a-zA-Z0-9])?.) + [A-zA-Z] {2, 6}

Second-level domain name not matched

 

Regular 6

The code is as follows: Copy code

([A-z0-9] [a-z0-9-] *?. (? : Com | cn | net | org | gov | info | la | cc | co )(? :.(? : Cn | jp ))?) $

Many top-level domain names do not match

Example

The code is as follows: Copy code

<? Php
$ Search = '~ ^ ([^ :/? #] + ):)? (// ([^ /? #] *)? ([^? #] *) (? ([^ #] *)? (#(.*))?~ I ';
$ Url = 'http: // www.111cn.net/pub/ietf/uri/#gonn ';
$ Url = trim ($ url );
Preg_match_all ($ search, $ url, $ rr );
Printf ("<p> output URL data: </p> <pre> % s </pre> n", var_export ($ rr, TRUE ));

/*
The groups are as follows:
$1 = http:
$2 = http
$3 = // www.111cn.net
$4 = www.111cn.net
$5 =/pub/ietf/uri/
$6 = <undefined>
$7 = <undefined>
$8 = # Gonn
$9 = Gonn
*/
?>

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.