Jump to content

[SOLVED] Preg_match fix?


Recommended Posts

Hello.

 

I currently use this to check if they have submitted a valid domain:

 

preg_match ("/^[a-z0-9][a-z0-9\-]+[a-z0-9](\.[a-z]{2,4})+$/i", $domain)

 

Works for example google.com but not for example mail.google.com.

 

So if there an easy way to accept subdomains too?

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/161262-solved-preg_match-fix/
Share on other sites

Hello.

 

I currently use this to check if they have submitted a valid domain:

 

preg_match ("/^[a-z0-9][a-z0-9\-]+[a-z0-9](\.[a-z]{2,4})+$/i", $domain)

 

Works for example google.com but not for example mail.google.com.

 

So if there an easy way to accept subdomains too?

 

Thanks.

 

i think the problem is that in "mail.google.com" you encounter "." (dot) two times , while your regex is set to check domains which have only one dot.

 

I'm kinda newbie with regular expressions but I will try to make one which works.

 

edit : ah..I hadn't refreshed page, it seems someone has answered your question. sry for useless post.

Hello.

 

I currently use this to check if they have submitted a valid domain:

 

preg_match ("/^[a-z0-9][a-z0-9\-]+[a-z0-9](\.[a-z]{2,4})+$/i", $domain)

 

Works for example google.com but not for example mail.google.com.

 

So if there an easy way to accept subdomains too?

 

Thanks.

 

I modified rivan regex to:

preg_match ("/^[a-z0-9][a-z0-9]*[\-]?+[a-z0-9]*(\.[a-z0-9][a-z0-9\-]+[a-z0-9])*(\.[a-z]{2,4})+$/i", $domain)

-because his regex wouldn't match domains starting with one letter before the first dot. (eg. a.google.com)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.