views:

31

answers:

4

Hi Friends

http://en.wordpress.com/tag/1000-things-we-hate/
http://en.wordpress.com/tag/1019/
http://en.wordpress.com/tag/1030-am/
http://www.yahoo.com/index.html
http://www.msn.com/index.html
i am having list of domains as above in my DB, i need to get only domain name from this, how can i do that in mysql or in JAVA or in PHP ??

A: 
<?php
$url = "http://en.wordpress.com/tag/1000-things-we-hate/";
$bits = explode("/",$url);
$nextBits = explode(".",$bits[1]);
$count = count($nextBits);
$domain = $nextBits[$count-1].".".$nextBits[$count];
echo $domain;
?>
Thomas Clayson
There are bugs in that code. It doesn't work as intended.Even if it would work as intended, it would not be sufficient. "http://bbc.co.uk/" would return "co.uk"
Sam
ah... yeah... didn't think about that. good spot.
Thomas Clayson
A: 
<?php
echo parse_url($url, PHP_URL_HOST);

That would return "en.wordpress.com". If you don't want subdomains (i.e. only "wordpress.com), then things are getting complicated. You would need something like http://www.dkim-reputation.org/regdom-libs/

Sam
A: 

Use the parse_url in PHP.

gsharma
+1  A: 

Here's a way to do it in Java:

String input = "http://en.wordpress.com/tag/1000-things-we-hate/";
// Assuming that all urls start with "http://"
int finish = input.indexOf("/", 7);
if(finish == -1)
{
  finish = input.length();
}
System.out.println(input.substring(7, finish));

Prints en.wordpress.com (I assume that is what you want?)

Catchwa