Home  >  Article  >  Backend Development  >  PHP gets the top-level domain name function code of the URL_PHP tutorial

PHP gets the top-level domain name function code of the URL_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:16:331099browse

PHP's top-level domain name function for obtaining URLs

The current international top-level domain names are: com|edu|gov|int|mil|net|org|biz|info|pro|name|museum|coop|aero|xxx|idv |mobi|cc|me
There are many regional domain names. They should be found in every country and region. You can collect them if you are interested. However, as far as I know, these domain names are composed of 2 letters. They can be used alone or alone. Use regional domain names in combination with international top-level domain names, such as:
cn China
tw Taiwan
hk Hong Kong

Domain name examples:
jb51.cn
baidu.com
jb51.com.cn

Take jb51.dom.cn as an example: phpwind (custom part).com (international domain name part).cn (regional domain name part)

Get the top-level domain name PHP function

Copy code The code is as follows:

function getdomain($url) {
$host = strtolower ( $url );
if (strpos ( $host, '/' ) !== false) {
$parse = @parse_url ( $host );
$host = $parse ['host'] ;
}
$topleveldomaindb = array ('com', 'edu', 'gov', 'int', 'mil', 'net', 'org', 'biz', 'info', ' pro', 'name', 'museum', 'coop', 'aero', 'xxx', 'idv', 'mobi', 'cc', 'me' );
$str = '';
foreach ( $topleveldomaindb as $v ) {
$str .= ($str ? '|' : '') . $v;
}

$matchstr = "[^. ]+.(?:(" . $str . ")|w{2}|((" . $str . ").w{2}))$";
if (preg_match ( "/" . $matchstr . "/ies", $host, $matchs )) {
$domain = $matchs ['0'];
} else {
$domain = $host;
}
return $domain;
}


Usage example:

Copy code The code is as follows :

$str = "http://www.jb51.net/tools/zhengze.html";
echo getdomain ( $str );

Output: jb51.net

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325934.htmlTechArticlePHP’s top-level domain name function to obtain the URL. The current international top-level domain names are: com|edu|gov|int|mil|net |org|biz|info|pro|name|museum|coop|aero|xxx|idv|mobi|cc|me There are many regional domain names, it should be...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn