Home  >  Article  >  Backend Development  >  Several methods to obtain domain names in PHP

Several methods to obtain domain names in PHP

WBOY
WBOYOriginal
2016-07-25 08:54:571509browse
  1. //Method 1 (using system variables) 

  2. //Disadvantages of not using the passed address and hosts that do not support system variables

  3. echo $_SERVER[ 'HTTP_HOST']; 

  4. //Method 2 (use built-in function) 

  5. $url = 'http://www.51php.net/index.php?referer=51php.net' ;​​
  6. $arr_url = parse_url($url); ​
  7. echo $arr_url['host']; $url = str_replace('http://',",$url); //If there is an http prefix, remove it
  8. $pos = strpos($url,'/');
  9. if($pos === false )  
  10.   {  
  11.     return $url;  
  12.   }else  
  13.   lt;p>// Method 4 (using regular expressions)
  14. preg_match("/^(http://)?([^/]+)/i", $url, $arr_domain);
  15. echo $arr_domain[2];

  16. Copy code
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