This article mainly introduces PHP methods for obtaining domain names. examples summarize common methods for obtaining domain names, which are very useful, for more information about how to obtain a domain name in PHP, see the examples in this article. Share it with you for your reference. The specific implementation method is as follows:
Method 1 (using system variables)
The code is as follows:
// Disadvantage: Do not use the passed address or host that does not support system variables
Echo $ _ SERVER ['http _ host'];
Method 2 (using built-in functions)
The code is as follows:
$ Url = 'http: // www.jb51.net/index.php? Referer = jb51.net ';
$ Arr_url = parse_url ($ url );
Echo $ arr_url ['host'];
Method 3 (write functions by yourself)
The code is as follows:
Function getdomain ($ url)
{
$ Url = str_replace ('http: // ', ", $ url); // remove the http prefix
$ Pos = strpos ($ url ,'/');
If ($ pos = false)
{
Return $ url;
} Else
{
Return substr ($ url, 0, $ pos );
}
}
Echo getdomain ($ url );
Method 4 (using regular expressions)
The code is as follows:
Preg_match ("/^ (http ://)? ([^/] +)/I ", $ url, $ arr_domain );
Echo $ arr_domain [2];
I hope this article will help you with PHP programming.