How to get only hub domain name from url

I have many thousands of urls from which I want to get the domain name like

http://google.com

<?php

$url = 'http://google.com';
$host = parse_url($url);
echo '<pre>';
print_r($host['host']);
echo '</pre>';

**//Output google.com**

?>

      

but i want to get google from http://google.com not google.com

please help thanks

+3


source to share


4 answers


Not particularly elegant, but something like this is just a domain name ...



$url = 'http://dev.subdomain.google.com';
$host = parse_url($url,PHP_URL_HOST);
$pieces=explode( '.', $host );
$popped=array_pop( $pieces ); //remove tld extension from stack
if( strlen( $popped ) <= 3 ) array_pop( $pieces ); //tld was likely a multi-part ext like .co.uk so pop next element off stack too!

$domain=array_pop( $pieces );

echo $domain; // returns 'google'

      

+2


source


$url = 'http://google.com';
$host = parse_url($url);
$host = strstr($host, '.com', true);

      



See php.net/strstr for more details , of course there are other and correct ways to solve them.

0


source


maybe you can fix it with regex

$host = (preg_replace("#(http://)|(https://)|\.(com)|(co\.uk)|(fr)|(de)|(org)|(net)#", "", $host));

      

preg_replace: preg_replace statement (php.net) check your regex: Debuggex

0


source


Try to enter the code

<?php
   $full_url = parse_url('http://facebook.com');
   $url = $full_url['host'];
   $url_array = explode('.',$url);
   echo $url_array[0];
?>

      

0


source







All Articles