Heim  >  Artikel  >  Backend-Entwicklung  >  探讨:PHP获取域名及域名IP地址的方法

探讨:PHP获取域名及域名IP地址的方法

WBOY
WBOYOriginal
2016-07-25 08:58:051132Durchsuche
  1. //全局数组
  2. echo $_SERVER[“HTTP_HOST”];
  3. //则会输出bbs.it-home.org
复制代码

本地测试则会输出localhost。

方法2,使用parse_url函数;

<?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 "
"; ?>

输出为数组,结果为:

Array ( [scheme] => http [host] => bbs.it-home.org [path] => /index.php [query] => referer=jbxue.com )

说明: scheme对应着协议,host则对应着域名,path对应着执行文件的路径,query则对应着相关的参数;

方法3,采用自定义函数。

<?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);
        }
    }
?>

方法4,使用php正则表达式。

<?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];
    }
?>
您可能感兴趣的文章: PHP获取域名的几个全局变量 php 实现dns域名查询的方法详解(图文) php 从url中获取域名的实例代码 php获取站点的来路域名的方法 php获取URL中domain域名的代码一例 PHP正则匹配获取URL中域名的代码 PHP获取当前网址及域名的代码 php正则表达式匹配URL中的域名 PHP调用万网接口实现域名查询的功能


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn