3

I have many thousands of urls from which i only want to get name of domain for example

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 only want to get google from http://google.com not google.com

please help thanks

Sufyan
  • 506
  • 2
  • 6
  • 18
  • try this preg_match('/(.*?)((\.co)?.[a-z]{2,4})$/i') – Insane Skull Jul 15 '15 at 08:09
  • This is *practically* impossible as realistically you need to know every possible TLD (and second level domains like org.uk) ... and a duplicate : http://stackoverflow.com/questions/18515261/parsing-domain-name-only-from-url-in-php/18515984#18515984 – CD001 Jul 15 '15 at 08:13
  • You mighty want to look into http://stackoverflow.com/questions/3211411/how-can-i-get-the-base-domain-name-from-a-url-using-php-eg-google-com-from-ima – chocochaos Jul 15 '15 at 08:21

4 Answers4

2

Not particularaly elegant but something like this gets simply the 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'
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
  • ..just realised this does not handle tld's like .co.uk as you would be left with 'co' rather than google..... – Professor Abronsius Jul 15 '15 at 08:21
  • thank u for answer but please help me to solve this tld's problem i have domain with such .co.uk – Sufyan Jul 15 '15 at 08:24
  • The last change gets much closer if there are typcial tld's like .co.uk or .gov.uk etc but with the newer types allowed ( as can be found in the list at https://wiki.mozilla.org/TLD_List ) the problem gets much more complicated. – Professor Abronsius Jul 15 '15 at 08:51
  • As an aside - here's the current TLD list you'd actually need to parse to be accurate : https://publicsuffix.org/list/public_suffix_list.dat - there are comments and spaces in it so it needs some work to parse but you're probably still looking at going on 10000 possible TLDs (or 2nd, 3rd, 4th level domains) – CD001 Jul 15 '15 at 08:53
0
$url = 'http://google.com';
$host = parse_url($url);
$host = strstr($host, '.com', true);

See php.net/strstr for more detailed information, of course there's other and properly better ways to do it.

Epodax
  • 1,828
  • 4
  • 27
  • 32
  • i have thousands of domain which not only have .com its have many varieties like .es .to .org .net etc so how it can be done? – Sufyan Jul 15 '15 at 08:17
0

maybe you can fix it with a regex

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

preg_replace : preg_replace manual (php.net) test your regex : Debuggex

Antoine Pointeau
  • 399
  • 2
  • 14
  • OK - now account for this : https://wiki.mozilla.org/TLD_List ... and that doesn't cover all secondary domains. – CD001 Jul 15 '15 at 08:17
  • True, it does't cover all extensions. You need to add them as usage. An other way is to remove all the content from the end to the last break point. This solution doesn't cover all extension to like ".co.uk" – Antoine Pointeau Jul 15 '15 at 08:25
  • I know - this question is a duplicate ... I've tried to answer it previously which is when I decided it's practically impossible to make a 100% effective solution ;) – CD001 Jul 15 '15 at 08:29
0

Try below code

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