PHP Regular Expression matches the domain name in the URL

Source: Internet
Author: User

On the PHP official website, we can see the replacement of the parse_url () function. The result is similar to that of the parse_url () function, which is implemented using regular expressions.

Original article: http://www.php.net/parse_url#104958

 

I will not translate it, it can parse URI

Uri is a type of resources available on the web-HTML documents, images, video clips,Program-Is located by an Uniform Resource Identifier ("Uri.

Grouping:

^ ([^ :/? #] + ):)? (// ([^ /? #] *)? ([^? #] *) (\? ([^ #] *)? (#(.*))?
12 3 4 5 6 7 8 9

PHP testing:

<? PHP  $ Search = '~ ^ ([^ :/? #] + ):)? (// ([^ /? #] *)? ([^? #] *) (\? ([^ #] *)? (#(.*))?~ I';  $ URL = 'HTTP: // www.php.net/pub/ietf/uri/#related' ;  $ 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.php.net $4 = www.php.net $5 =/pub/IETF/uri/$6 = <undefined> $7 = <undefined> $8 = # related $9 = related  */ ?>

Baidu sees another simpleCode:

<?PHP//Obtain the host name from the URLPreg_match("/^ (Http :\/\/)? ([^ \/] +)/I "," http://www.php.net/index.html ",$ Matches);$ Host=$ Matches[2];//Obtain the following two segments from the host namePreg_match("/[^ \. \/] + \. [^ \. \/] + $ /",$ Host,$ Matches);Echo"Domain name is :{$ Matches[0]} \ n";?>

Output after execution:Domain name is: php.net

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.