PHP Regular Expressions get the domain name from the URL _php tutorial

Source: Internet
Author: User
The following code can get the domain name from the URL

Example: http://bkjia.com/live/q.html

Get bkjia.com

The following is the referenced content:
function Get_domain ($url) {
$pattern = "/[\w-]+\. (COM|NET|ORG|GOV|CC|BIZ|INFO|CN) (\. (CN|HK)) */";
Preg_match ($pattern, $url, $matches);
if (count ($matches) > 0) {
return $matches [0];
}else{
$rs = Parse_url ($url);
$main _url = $rs ["Host"];
if (!strcmp (Long2ip (sprintf ("%u", Ip2long ($main _url))), $main _url)) {
return $main _url;
}else{
$arr = Explode (".", $main _url);
$count =count ($arr);
$ENDARR = Array ("com", "net", "org", "3322"),//com.cn net.cn, etc.
if (In_array ($arr [$count-2], $ENDARR)) {
$domain = $arr [$count-3]. ".". $arr [$count-2]. ".". $arr [$count-1];
}else{
$domain = $arr [$count-2]. ".". $arr [$count-1];
}
return $domain;
}//End If (!strcmp ...)
}//End If (count ...)
}//End Function

http://www.bkjia.com/PHPjc/363986.html www.bkjia.com true http://www.bkjia.com/PHPjc/363986.html techarticle The following code can get the domain name from the URL for example: http://liehuo.net/live/q.html get liehuo.net The following: function Get_domain ($url) {$pattern =/[\ W-]+\. (com|net|org ...

  • Related Article

    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.