0

Please consider the below list of domains suffix:

COM .CO .INFO .NET .ORG .US .CA .MOBI .BIZ .ME .CC .NAME .TV .WS .DE .AM .FM .BZ .COM.BZ .NET.BZ .ES .ASIA .SE .XXX

I need to extract the only suffix part from above domains list. For example, If I have 'www.google.com', 'www.backforgood.us', 'www.birdslisten.org'... and etc, the only part I need are 'com', 'us', 'org'?

I need to cover all the domain suffix mentioned in above list. Is it possible with single regex? Or any other good solution will be helpful? Thanks

4
  • What if www.example.co.in comes.You want .in or .co.in Aug 24, 2013 at 8:43
  • the regex part is explained here stackoverflow.com/questions/9267389/…
    – gherkins
    Aug 24, 2013 at 8:45
  • @User016, I want '.co.in', the complete suffix part. As my list contains 'COM.BZ'
    – Irfan
    Aug 24, 2013 at 8:52
  • 1
    @IrfanDayan see my updated answer you will have your solution. Aug 24, 2013 at 9:46

2 Answers 2

2

You can get .co.in and .co.uk suffix using below code.

Example

$host = 'http://www.google.co.in';
//$host = 'http://www.google.co.uk';
preg_match('/(.*?)((\.co)?.[a-z]{2,4})$/i', $host, $m);
echo $ext = isset($m[2]) ? $m[2]: '';

code is tested for .co.in and .co.uk

And for .com.biz you need to alter regular expression a bit.

$host = 'http://www.google.com.biz';
preg_match('/(.*?)((\.co[a-zA-Z])?.[a-z]{2,4})$/i', $host, $m);
echo $ext = isset($m[2]) ? $m[2]: '';
0
0

You can use something like this if you want the last part of the domain

$name = 'www.birdslisten.org';
$array = explode(".", $name);
end($array);
$suffix = current($array);
echo $suffix;
2
  • @DevangRathod, Yes It should include '.co.in'. I need the complete suffix part of domain. As my list contains 'COM.BZ'
    – Irfan
    Aug 24, 2013 at 8:55
  • @User016 You already said yourself that what about .co.in and you are posting answer without checking ? Aug 24, 2013 at 8:55

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.