How to check a domain name using Regex & Php?

I want a solution to check only domain names not full URLs. The following example is what I'm looking for:

domain.com -> true domain.net -> true domain.org -> true domain.biz -> true domain.co.uk -> true sub.domain.com -> true domain.com/folder -> false domµ*$ain.com -> false 

thank

+16
php regex preg-match
Jun 12. '10 at 0:00
source share
6 answers

What about:

 ^(?:[-A-Za-z0-9]+\.)+[A-Za-z]{2,6}$ 
+17
Jun 12 2018-10-12T00:
source share

The selected answer is incomplete / incorrect.

Regular expression pattern

  • Should NOT check domains, for example:
    -domain.com , domain--.com , -domain-.-.com , domain.000 etc.

  • domains should be checked, for example:
    schools.k12 , newTLD.clothing , good.photography etc.

After further research; below is the most correct, cross-language and compact drawing that I could come up with:

 ^(?!\-)(?:[a-zA-Z\d\-]{0,62}[a-zA-Z\d]\.){1,126}(?!\d+)[a-zA-Z\d]{1,63}$ 

This template complies with most * rules defined in the specifications:

  • Each label / level (separated by a period) can contain up to 63 characters .
  • A fully qualified domain name can have up to 127 levels .
  • A fully qualified domain name cannot exceed 253 characters in its textual representation.
  • Each label can consist of letters, numbers, and hyphens .
  • Shortcuts cannot start or end with a hyphen.
  • Top-level domain (extension) cannot be all-numeric .

Note 1 A full domain length check is not included in the regular expression. It should just be verified by native methods, for example. strlen(domain) <= 253 .
Note 2 This template works with most languages, including PHP, Javascript, Python, etc.

See DEMO here (for JS, PHP, Python)

Additional Information:

  • The regular expression above does not support IDN s.

  • There is no specification indicating that the extension (TLD) should be between 2 and 6 characters. It actually supports 63 characters. See the current TLD list here . In addition, some networks internally use user / pseudo-TLDs.

  • Registration authorities may impose additional, specific rules that are not explicitly supported in this regular expression. For example, .CO.UK and .ORG.UK must have at least 3 characters, but less than 23, not including the extension. These rules are non-standard and subject to change. Do not follow them if you cannot support.

  • Regular expressions are a great, but not the best, effective, effective solution to every problem. Therefore, you should use your own URL parser instead. e.g. Python urlparse() or PHP parse_url() ...

  • In the end, it's just a format check. The regex test does not confirm that the domain name is really configured / exists! You must verify existence by making a request.

Specifications and references:

+60
May 10 '13 at 9:27
source share

In my case, the domain name is considered valid if the format is stackoverflow.com or xxx.stackoverflow.com

Thus, in addition to other answers to the stack, I added a check to www. also.

 function isValidDomainName($domain) { if (filter_var(gethostbyname($domain), FILTER_VALIDATE_IP)) { return (preg_match('/^www./', $domain)) ? FALSE : TRUE; } return FALSE; } 

you can check the function with this code

  $domain = array("http://www.domain.com","http://www.domain.com/folder" ,"http://domain.com", "www.domain.com", "domain.com/subfolder", "domain.com","sub.domain.com"); foreach ($domain as $v) { echo isValidDomainName($v) ? "{$v} is valid<br>" : "{$v} is invalid<br>"; } 
+1
Jul 16 '14 at 6:02
source share

Try the following expression:

 ^(http[s]?\:\/\/)?((\w+)\.)?(([\w-]+)?)(\.[\w-]+){1,2}$ 

What is he really doing

  • optional http / s: //
  • optional www
  • any valid alphanumeric name (including - and _)
  • 1 or 2 occurrences of any valid alphanumeric name (including - and _)

Validation Examples

+1
Mar 30 '16 at 18:39
source share

Remember that regular expressions can only check if something is correctly formed. "www.idonotexistbecauseiammadeuponthespot.com" is well-formed, but doesn’t really exist ... at the time of writing .;) In addition, some free hosting providers (like Tripod) allow you to underline in subdomains. This is clearly a violation of the RFC, but it sometimes works.

Do you want to check if a domain exists? Try dns_get_record instead of (just) regex.

0
Jun 12 '10 at 2:13
source share

I made a function to check the domain name without any regular expression.

 <?php function validDomain($domain) { $domain = rtrim($domain, '.'); if (!mb_stripos($domain, '.')) { return false; } $domain = explode('.', $domain); $allowedChars = array('-'); $extenion = array_pop($domain); foreach ($domain as $value) { $fc = mb_substr($value, 0, 1); $lc = mb_substr($value, -1); if ( hash_equals($value, '') || in_array($fc, $allowedChars) || in_array($lc, $allowedChars) ) { return false; } if (!ctype_alnum(str_replace($allowedChars, '', $value))) { return false; } } if ( !ctype_alnum(str_replace($allowedChars, '', $extenion)) || hash_equals($extenion, '') ) { return false; } return true; } $testCases = array( 'a', '0', 'a.b', 'google.com', 'news.google.co.uk', 'xn--fsqu00a.xn--0zwm56d', 'google.com ', 'google.com.', 'goo gle.com', 'a.', 'hey.hey', 'google-.com', '-nj--9*.vom', ' ', '..', 'google..com', 'www.google.com', 'www.google.com/some/path/to/dir/' ); foreach ($testCases as $testCase) { var_dump($testCase); var_dump(validDomain($TestCase)); echo '<br /><br />'; } ?> 

This code outputs:

string (1) "a" bool (false)


string (1) "0" bool (false)


string (3) "ab" bool (true)


string (10) "google.com" bool (true)


string (17) "news.google.co.uk" bool (true)


line (23) "xn - fsqu00a.xn - 0zwm56d" bool (true)


string (11) "google.com" bool (false)


string (11) "google.com." bool (true)


line (11) "goo gle.com" bool (false)


string (2) "a." bool (false)


string (7) "hey.hey" bool (true)


string (11) "google-.com" bool (false)


string (11) "-nj - 9 * .vom" bool (false)


string (1) "" bool (false)


string (2) ".." bool (false)


line (11) "google..com" bool (false)


string (14) "www.google.com" bool (true)


line (32) "www.google.com/some/path/to/dir/" bool (false)

I hope I considered everything, if I missed something, please tell me, and I can improve this function. :)

0
Jun 25 '17 at 10:08
source share



All Articles