-1

I'm trying to figure out why a domain name (example1.commerce) isn't matching the following regular expression:

if (!preg_match('([a-z\d]){1,63}(\.)([a-z\d]){2,16}',$_POST['domain']))
{
 echo '<p>Error: domain is not valid.</p>';
}
else {echo '<p>Domain looks good.</p>';}

The pattern should only match the domain (1~63 characters) and domain suffix (2~16 characters); no subdomains, uppercase, etc.

All the online regex testers seem to work though

John
  • 1
  • 13
  • 98
  • 177
  • 3
    Use regex delimiters - `'/([a-z\d]){1,63}(\.)([a-z\d]){2,16}/'`. And perhaps, anchors and `/i` modifier and remove redundant groups: `'/^[a-z\d]{1,63}\.[a-z\d]{2,16}$/i'` – Wiktor Stribiżew Sep 22 '16 at 19:25
  • @WiktorStribiżew Ah, please post as an answer and I'll accept. I think this was missing from someone' post when I combined the expression with `preg_match` for some reason. – John Sep 22 '16 at 19:29
  • Do you mean to say the regex delimiters were missing? That is all? Or are the anchors / case insensitive modifier are also necessary? Please confirm. – Wiktor Stribiżew Sep 22 '16 at 19:30
  • @WiktorStribiżew Just didn't have the slashes. I have limited the number of regular expressions in PHP (and always after all other validation) so I guess the lack of repetition is what got me. – John Sep 22 '16 at 19:32
  • Didn't you get a *Unknown modifier '{'* warning? See http://ideone.com/jgg8j4 – Wiktor Stribiżew Sep 22 '16 at 19:35
  • @WiktorStribiżew Odd, I have a log viewer for SQL queries and looking at it now I see the PHP errors being written to my DB however the log viewer didn't trigger the audio signal, W-T-H right? >__> – John Sep 22 '16 at 19:43
  • Ok, that is it. You had the warning, and you should have mentioned it here in the question. – Wiktor Stribiżew Sep 22 '16 at 19:43
  • @WiktorStribiżew If you think it's a duplicate fine, but that thread doesn't correlate to what I asked and if it does it sure as heck is not asked in a proper context. Either find a *proper* existing thread to mark this as a duplicate of or remove the duplicate marker. – John Sep 27 '16 at 03:20

1 Answers1

2

'([a-z\d]){1,63}(\.)([a-z\d]){2,16}'

should be

'/([a-z\d]){1,63}(\.)([a-z\d]){2,16}/i'

Most online regex testers add this here for you.

Goose
  • 4,764
  • 5
  • 45
  • 84
  • @WiktorStribiżew yes, but it should be posted as an answer for future users. Since it was your comment, if you post this as an answer I will delete mine. – Goose Sep 22 '16 at 19:40
  • @WiktorStribiżew most likely. Have you found the dupe and can you post it as a comment? – Goose Sep 22 '16 at 19:42