Remove domain extension

前端 未结 5 1160
情书的邮戳
情书的邮戳 2020-11-30 14:10

So let\'s say I have just-a.domain.com,just-a-domain.info,just.a-domain.net how can I remove the extension .com,.net.info ... and I need the result

5条回答
  •  情歌与酒
    2020-11-30 14:50

      preg_match('/(.*?)((?:\.co)?.[a-z]{2,4})$/i', $domain, $matches);
    

    $matches[1] will have the domain and $matches[2] will have the extension

    
    

    Will produce the output

    Array
    (
        [0] => google.com
        [1] => google
        [2] => .com
    )
    Array
    (
        [0] => google.in
        [1] => google
        [2] => .in
    )
    Array
    (
        [0] => google.co.in
        [1] => google
        [2] => .co.in
    )
    Array
    (
        [0] => google.info
        [1] => google
        [2] => .info
    )
    Array
    (
        [0] => analytics.google.com
        [1] => analytics.google
        [2] => .com
    )
    

提交回复
热议问题