views:

65

answers:

2

How to remove www and validate a valid domain name.

Valid Domain

domain.com
subdomain.domain.com
sub-domain.domain.com

Invalid Domain

www.domain.com
www.subdomain.domain.com
http://www.domain.com/
http://www.subdomain.com/
www.domain.com/folder/

How the code?

$domain ='www.domain.com/folder/';
if() {
// validate here
}
+1  A: 

First, manually strip out the www. then just make sure it is in the form domain.tld or something.domain.tld

$domain = str_replace('www.','',$domain);

if(preg_match('/([a-zA-Z0-9\-_]+\.)?[a-zA-Z0-9\-_]+\.[a-zA-Z]{2,5}/',$domain)) //valid domain

You seem to be getting your `.` and `,` mixed up there … and TLDs are not limited to 4 characters … and the question appears to ask that hostnames with `www` be rejected, not normalised.
David Dorward
Typos were already fixed. Down-voting for them is worthless.
`/^[a-z0-9][a-z0-9\-]+[a-z0-9](\.[a-z]{2,5})+$/i` working
bob
something.museum? 6 chars in the TLD. You could also add another check, the domain name and the sub domain name must have at most 63 chars.
Alix Axel
A: 

Take a look at http://framework.zend.com/apidoc/core/Zend_Validate/Zend_Validate_Hostname.html (Zend_Validate_Hostname) class. I think that you can use it outside of Zend Framework, with a minor tuning.

Vincenzo