0

Suppose i have given URL, i want to get only domain name. How do i achieve this in Php.

    +----------------------+------------+
    | input                | output     |
    +----------------------+------------+
    | www.google.com       | google     |
    | www.mail.yahoo.com   | mail.yahoo |
    | www.mail.yahoo.co.in | mail.yahoo |
    | www.abc.au.uk        | abc        |
     www.subdoamin.domain.co.in     // output subdomain

I applied the follwing trick but fails when i have TLD like "co.uk"

     if(isset($project_detail_all[0]->d_name)) {
        $domain_name = $project_detail_all[0]->d_name ;
        $domain_name = explode('.', $domain_name);
        $count = count($domain_name);
        if (top_level_domains($domain_name[$count-1]) && 
            stristr($rss_url, $domain_name[$count-2])) {
            return  isValidXML($rss_url);
        } else {
            return  ['status'=>false , 'invalid_Domain'=>true];
        }
    } else {
        return  ['status'=>false , 'invalid_Domain'=>true];
    }

Kindly help me

7
  • Look into this library function, this may help you very much! parse_url Commented May 19, 2017 at 11:41
  • <?php $domain_name = 'abc.news18.co.in/abc.php' ; $ParsedURL = parse_url(strtolower($domain_name)); if ($ParsedURL['host'] == null) { $domain_name = 'http://'.$domain_name; $ParsedURL = parse_url($domain_name); $domain_name = explode('.', str_ireplace('www.', '', $ParsedURL['host'])); } else { $domain_name = explode('.', str_ireplace('www.', '', $ParsedURL['host'])); } print_r( $domain_name); Commented May 19, 2017 at 11:47
  • parse url will not work in this case Commented May 19, 2017 at 11:49
  • you need 'abc.news18' from the url, right? Commented May 19, 2017 at 11:53
  • no i want only new18 Commented May 19, 2017 at 12:04

2 Answers 2

2

What you are looking for is parse_url()

<?php

    $url = parse_url("http://abc.news18.co.in");
    //echo $url['host'];
    echo preg_replace("/^([a-zA-Z0-9].*\.)?([a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9]\.[a-zA-Z.]{2,})$/", '$2', $url['host']); 

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

8 Comments

$domain_name = 'abc.news18.co.in/abc.php';
I added a regex which strips the subdomain.
its giving me news18.co.in , but i want only new18 and tld ('co.in') respectively
and again if url does not containing " http:// " parse_url will not work
Then split the result: $pieces = explode(" ", $url['host']); echo $pieces[1];
|
1

please use regex to get only domain.

/.*?\.([A-z\d\.]+)(.co|.au)/s 

You can see my solution result here. green colored text is result of regex.

Use below code.

$urls = ['www.google.com','www.mail.yahoo.com','www.mail.yahoo.co.in','www.abc.au.uk','www.subdoamin.domain.co.in','abc.news18.co.in/abc.php'];
$result = [];
foreach($urls as $url){
   preg_match('/.*?\.([A-z\d\.]+)(.co|.au)/s',$url,$match);
   $result[] = $match[1];
}
echo '<pre>';print_r($result);

And you'll get result whatever you want.

1 Comment

It was not working before, now I've changed my answer and regex so it will work now, please open new updated link and you can get idea,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.