This article mainly introduces the PHP method for obtaining domain names. The example summarizes the common method for obtaining domain names, which is very useful. For more information, see
This article mainly introduces the PHP method for obtaining domain names. The example summarizes the common method for obtaining domain names, which is very useful. For more information, see
This example summarizes the PHP method for obtaining domain names. 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 ("/^ ()? ([^/] +)/I ", $ url, $ arr_domain );
Echo $ arr_domain [2];
I hope this article will help you with PHP programming.
,