Hi all If I have many urls, mail.google.com.pk mail.google.com google.com google.com.pk how I get only google in output, please suggest me idea
This is sorta ambiguous - do you need this to recognize any TLD, and only the domain itself? Note: a "domain" in this setting is not just the name "Google" - it's actually "Google.com" However, this code does what you want on the example above: <?php $domain_array = array('mail.google.com.pk','mail.google.com','google.com','google.com.pk'); foreach($domain_array as $key => $value) { $tld_array = array('com','com.pk','net','org','me','co.uk','no','se'); $url_ex = array_reverse(explode('.',$value)); if (!in_array($url_ex[0],$tld_array)) { if (!in_array($url_ex[1].'.'.$url_ex[0],$tld_array)) { echo 'Couldn\'t find TLD'; } else { echo $url_ex[2].PHP_EOL; } } else { echo $url_ex[1].PHP_EOL; } } ?> PHP: However, you will need to update the $tld_array to match more TLDs, if needed.