Home >Backend Development >PHP Tutorial >Discussion: How to obtain domain name and domain name IP address with PHP
The local test will output localhost. Method 2, use parse_url function; <?php $url ="http://bbs.it-home.org/index.php?referer=jbxue.com"; $arr=parse_url($url); echo "<pre class="brush:php;toolbar:false">"; print_r($arr); echo ""; ?> The output is an array, and the result is: Array ( [scheme] => http [host] => bbs.it-home.org [path] => /index.php [query] => referer=jbxue.com )Instructions: Scheme corresponds to the protocol, host corresponds to the domain name, path corresponds to the path of the execution file, and query corresponds to the relevant parameters; Method 3, using a custom function. <?php $url ="http://bbs.it-home.org/index.php?referer=jbxue.com"; get_host($url); function get_host($url){ //首先替换掉http:// $url=Str_replace("http://","",$url); //获得去掉http://url的/最先出现的位置 $position=strpos($url,"/"); //如果没有斜杠则表明url里面没有参数,直接返回url, //否则截取字符串 if($position==false){ echo $url; }else{ echo substr($url,0,$position); } } ?> Method 4, use php regular expressions. <?php header("Content-type:text/html;charset=utf-8"); $url ="http://bbs.it-home.org/index.php?referer=jbxue.com"; $pattern="/(http:\/\/)?(.*)\//"; if(preg_match($pattern,$url,$arr)){ echo "匹配成功!"; echo "匹配结果:".$arr[2]; } ?>Articles you may be interested in: PHP obtains several global variables of the domain name Detailed explanation of the method of php to implement dns domain name query (picture and text) php example code to get domain name from url How to get the source domain name of the site with php An example of using php to obtain the domain name code in the URL PHP regular matching to obtain the code of the domain name in the URL PHP gets the code of the current website and domain name php regular expression matching domain name in URL PHP calls Wanwang interface to implement domain name query function |