What will be preg_match for domain names?

Discussion in 'PHP' started by JEET, Sep 28, 2010.

  1. #1
    Hi,
    I need a preg_match pattern that matches all these domain names, but I cannot seem to get it right. Can someone help please? Thanks :)

    something.firm.in
    something123.firm.in
    some-thing-123.firm.in
    something.net.in
    something.co.in
    something.co
    some-thing.com
    something.info
    some-thing.name
    so.com

    I need this for a function so if someone posts a domain name, then my php script can validate if it is actually a domain name. True/False etc.
    I think I have all possible combinations in that list, but if I missed something, please let me know.
    Thanks :)
     
    JEET, Sep 28, 2010 IP
  2. Media Signal

    Media Signal Peon

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    function parseDomain($domain)
    {
        preg_match("/^[a-zA-Z0-9-.]+(.com|.in|.co|.info|.name)$/", $domain, $matches);
        if ($matches) { return true; } else { return false; }
    }
    
    $domainList = array("joke_domain.com", "musli#.com", "something.firm.in", "something123.firm.in", "some-thing-123.firm.in", "something.net.in", "something.co.in", "something.co", "some-thing.com", "something.info", "some-thing.name", "so.com");
    
    foreach ($domainList as $domain)
    {
        if (parseDomain($domain) == true) {
        
            echo "$domain - VALID <br>";
        
        } else {
        
            echo "$domain - NOT a valid domain name <br>";
        
        }
    }
    PHP:
    [COLOR="red"]joke_domain.com - NOT a valid domain name
    musli#.com - NOT a valid domain name[/COLOR]
    [COLOR="green"]something.firm.in - VALID
    something123.firm.in - VALID
    some-thing-123.firm.in - VALID
    something.net.in - VALID
    something.co.in - VALID
    something.co - VALID
    some-thing.com - VALID
    something.info - VALID
    some-thing.name - VALID
    so.com - VALID[/COLOR]
    Code (markup):
     
    Media Signal, Sep 28, 2010 IP
  3. gapz101

    gapz101 Well-Known Member

    Messages:
    524
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    150
    #3
    ^^ how about xx...com , testasfasdfcom?
     
    gapz101, Sep 28, 2010 IP
  4. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #4
    Hi,
    Thanks for code above, but is it possible to modify it so I don't have to hard code domain extensions? Those are the main problem for me.

    preg_match("/^[a-zA-Z0-9-.]+(.com|.in|.co|.info|.name)$/", $domain, $matches);

    I think extensions follow these rules:
    1> It's only between a-zA-Z
    2> could "only" be between 2-4 chars long
    3> could have a second part seperated by a . sign
    4> The second part can be 2-4 chars long and is between a-zA-Z

    How would I modify the second part of the preg pattern above so it matches these 4 rules?
    Thanks :)
     
    JEET, Sep 28, 2010 IP
  5. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #5
    Hi,
    I think this works:

    $c='domain.com';
    preg_match('/^([a-zA-Z0-9-]{2,})+(\.[a-zA-Z]{2,4})*(\.[a-zA-Z]{2,4})$/is', $c, $match);

    Thanks :)
     
    JEET, Sep 28, 2010 IP
  6. Media Signal

    Media Signal Peon

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    preg_match("/^[a-zA-Z0-9]*((-|\.)?[a-zA-Z0-9])*\.([a-zA-Z]{2,4})$/", $domain, $matches); 
    PHP:
    Matches everything that should be matched :)
     
    Media Signal, Sep 28, 2010 IP