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 " Copy after login 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); } } ?> Copy after login 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]; } ?> Copy after login |