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
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):
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
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
preg_match("/^[a-zA-Z0-9]*((-|\.)?[a-zA-Z0-9])*\.([a-zA-Z]{2,4})$/", $domain, $matches); PHP: Matches everything that should be matched