domain matching regex

A regular expression that validates a given domain without its top level domain. I didnt need to validate the tld because this comes from a selectbox, so its predefined and always valid. The http protocol or the second level domain www. are both optional, it can be written or not.

$regex = "/^(?:(http:\/\/)?)(?:(w{3}\.)?)([A-Z0-9\-".utf8_encode("äÄöÖüÜ")."]{3,})$/i";
if(!preg_match($regex,$domainname)) {
	return false;
}

So the string can look like this:
http://www.liquidbass or www.liquidbass or http://liquidbass

detailed Description:

/^

This is the beginning of the regex. The ^ is used for searching all matches from the beginning of the given string.

(?:(http:\/\/)?)(?:(w{3}\.)?)

Here we have the optional protocol and the optional second level domain www. …

([A-Z0-9\-".utf8_encode("äÄöÖüÜ")."]{3,})

This part of the regex allows all characters from a to z, hyphens and all numerics from 0 to 9.
Additionally german special chars are allowed too.
All these possible characters must match a stringlength of minimum 3.

$/i

the dollar stands for the strings end, so it would be searched for matches from beginning to the end.
the /i stands for case sensivity - the given string can be in capital letters or in lowercase.

Leave a Reply