12 次查询 耗时 0.054 秒
本文共计496个字,0条留言,预计阅读时长2分钟
登录/注册
  • 首页
  • >
  • Notes
  • >
  • WordPress获取网址链接中的主域名
  • WordPress获取网址链接中的主域名

    作者:Misshylsay

    日期:2020年02月28日

    WordPress如何获取网址链接中的主域名?

    请将如下代码放入主题的function.php中:

    /*获取URL host*/
    function geturlhost($url){
        $url = strtolower($url);  //首先转成小写
        $hosts = parse_url($url);
        $host = $hosts['host'];
        // //查看是几级域名
        // $data = explode('.', $host);
        // $n = count($data);
        // //判断是否是双后缀
        // $preg = '/[\w].+\.(com|net|org|gov|edu)\.cn$/';
        // if(($n > 2) && preg_match($preg,$host)){
        // //双后缀取后3位
        // $host = $data[$n-3].'.'.$data[$n-2].'.'.$data[$n-1];
        // }else{
        // //非双后缀取后两位
        // $host = $data[$n-2].'.'.$data[$n-1];
        // }
        return $host;
    }

    调用方法:

    <?php echo geturlhost($ylinks);?>

    $ylinks 是原始网址

    “WordPress获取网址链接中的主域名”共有0条评论

    发表评论

    B em del U Link Code Quote