-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

2
  • i want to echo only the tld and https this question doesn't help me Commented Sep 17, 2018 at 22:42
  • protocol:RegExp.$2, tld: last element of RegExp.$3 split on '.' Commented Sep 18, 2018 at 0:43

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

can you explain more
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    

*/

2 Comments

how to echo only the $tld
this wil lnot work with names.co.uk

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.