如何从域回显域TLD

时间:2018-09-17 22:31:55

标签: php regex

我想从任何域中回显诸如.com或.net之类的域tld

如果我想回显此域https或http

示例:https://facebook.com

我要回显com和https

示例2:http://blockchain.info

我想回显信息和http

示例3:http://www.sub.test.info

我想回显信息和http

我已经尝试过

<?php

function get_domain($url)
{
  $pieces = parse_url($url);
  $domain = isset($pieces['host']) ? $pieces['host'] : $pieces['path'];
    
  if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
    return $regs['domain'];
  }
  return false;
}

echo get_domain("https://facebook.com");

?>

但这回响了facebook.com

编辑:我想回应域tld和https / http

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

<?php
function get_specs($url){
   $pieces = parse_url($url);
   $domain = isset($pieces['host']) ? $pieces['host'] : $pieces['path'];
   $tld = end(explode(".", $pieces['host'])); 
   return array("tld"=>$tld,"protocol"=>$pieces["scheme"]);
}

var_dump(get_specs('http://www.example.sub.com/site'));

/*
 results 

    tld => com,
    protocol => http    

*/