-1

I want to echo the domain tld such as .com or .net from any domain

althought I want to echo if this domain https or http

example : https://facebook.com

I want to echo com and https

example 2 : http://blockchain.info

I want to echo info and http

example 3 :http://www.sub.test.info

I want to echo info and http

I have tried this

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

?>

but this echo facebook.com

edit: I want to echo domain tld and the https/http

Cœur
  • 37,241
  • 25
  • 195
  • 267

2 Answers2

0

Use either strpos or substr to check if it's http or https and then use strrpos along with substr to get everything after the last dot.

hostingutilities.com
  • 8,894
  • 3
  • 41
  • 51
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    

*/
Sam Wise
  • 11
  • 3