[regex]how to grab the domain

Discussion in 'Programming' started by brainet, Sep 13, 2009.

  1. #1
    please help me, i want to grab only the domain from this sting
    http://www.somedomain.com/cool/somefile.php
    Code (markup):
    i want to grab the
    "http://www.somedomain.com"
    Code (markup):
    any regex would help?
     
    brainet, Sep 13, 2009 IP
  2. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    $text = 'http://www.somedomain.com/cool/somefile.php';
    
    if (preg_match('/(https?:\/\/[^\/]+)/i', $text, $match)) {
        echo $match[1];
    } else {
       echo 'not found';
    }
    
    //or alternatively:
    
    if ($parsed = parse_url($text)) {
    	echo 'http://' . $parsed['host'];
    } else {
       echo 'Incorrect URL';
    }
    
    PHP:
     
    premiumscripts, Sep 14, 2009 IP
  3. ohteddy

    ohteddy Member

    Messages:
    128
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    28
    #3
    Actually that previous regex is prone to errors.
    Make sure you anchor the regex by including a ^ at the start '/^(https.../
     
    ohteddy, Sep 14, 2009 IP
  4. Digital Linx

    Digital Linx Peon

    Messages:
    74
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Here's something more universal with perl;
    
    [root@node5 ~]# perl -le '$url = "http://site.me/some/file.html"; print $1 if $url =~ m/^([https]*:\/\/[\w_]+\.[\w]{2,4})\//;'
    http://site.me
    [root@node5 ~]# perl -le '$url = "https://site.me/some/file.html"; print $1 if $url =~ m/^([https]*:\/\/[\w_]+\.[\w]{2,4})\//;'
    https://site.me
    [root@node5 ~]# perl -le '$url = "https://site.com/some/file.html"; print $1 if $url =~ m/^([https]*:\/\/[\w_]+\.[\w]{2,4})\//;'
    https://site.com
    [root@node5 ~]# perl -le '$url = "https://site.info/some/file.html"; print $1 if $url =~ m/^([https]*:\/\/[\w_]+\.[\w]{2,4})\//;'
    https://site.info
    [root@node5 ~]# perl -le '$url = "https://site.ERROR/some/file.html"; print $1 if $url =~ m/^([https]*:\/\/[\w_]+\.[\w]{2,4})\//;'
    [root@node5 ~]#
    
    Code (markup):
     
    Digital Linx, Sep 15, 2009 IP