2

I have a webapp, where people signup and get a sub domain under this app domain ( xx.app.com ) ... for each subdomain there is a db that is attached to it grammatically and have the same name as the subdomain.

what i need is the right regex that works with the subdomain and off course a db name ( mysql if it matters ), it's supposed to be lowercase & the length between 6 and 20 and the only allowed character is the " - ", also numbers are banned ...

i tried many times but it always go bad, .. some like : /([a-z-]){6,20}/

Thanks in advance :)

Dewan159
  • 2,984
  • 7
  • 39
  • 43

4 Answers4

2

There might be a right regex for this, but regex isn't right for this.

Try parse_url

Edit:

I am not sure how you are using it. If you are only processing the subdomain part, the following should work and not match numbers:

^[a-z-]{6,20}$

This ensures that the subdomain has only a to z and - and between 6 and 20 times. The ^ matches the beginning of the string and $ matches the end.

The reason the earlier regex was accepting numbers or anything else too was because the match itself would have been a part of the string. Now with the ^ and $ you are ensuring that it is the entire string.

manojlds
  • 290,304
  • 63
  • 469
  • 417
  • why, i wanna check the chosen name first, – Dewan159 Apr 24 '11 at 20:09
  • whith parse_url, cannot get subdomain ! it return only hostname, for example for : http://xxx.domain.com, return host : xxx.domain.com – DJafari Apr 24 '11 at 20:10
  • there is no need to escape the dash/hyphen at that location in the character class, but you *can* if you really want to ;) – mousio Apr 24 '11 at 20:13
  • @mousio - right! Just jumped to conclusion that unescaped - is why it was not working. I have updated the regex which should work for him based on his comment that it is accepting numbers – manojlds Apr 24 '11 at 20:21
1

This would be a safer regex, since a subdomain cannot start with an hyphen:

^[a-z][a-z-]{5,19}$

As for the database name I believe it cannot contain an hyphen since it is the subtraction operator, so your best choice might be to either disallow hypens or replace them with underscores:

$database = str_replace('-', '_', $subdomain);

EDIT: Apparently @nikic is right, you can use hyphens as long as you backtick the database name.

Alix Axel
  • 151,645
  • 95
  • 393
  • 500
0

Have you tried escaping the hyphen?

/([a-z\-]){6,20}/
Halcyon
  • 57,230
  • 10
  • 89
  • 128
0

You will need positive lookahead regex for this. Try following code:

<?php
   $a = array("xx-yyy.domain.cam", "xx4yyy.domain.cam", "abcde.domain.com", "my-sub-domain.domain.org");
   foreach ($a as $v) {
      echo "For domain $v: ";
      preg_match('/^(?:[-a-z]{6,20})(?=\.)/', $v, $m );
      if (count($m) > 0)
         echo( "subdomain: " . $m[0] . "\n");
      else
         echo "subdomain not matched\n";
   }
?>

Basically match combination of lower case alphabets and hyphen - character of 6 to 20 character length before appearance of first dot . character.

- hyphen need not be escaped if used at the start in square brackets.

OUTPUT

For domain xx-yyy.domain.cam: subdomain: xx-yyy
For domain xx4yyy.domain.cam: subdomain not matched
For domain abcde.domain.com: subdomain not matched
For domain my-sub-domain.domain.org: subdomain: my-sub-domain
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Oh, that works too :), i should start learning some about regex – Dewan159 Apr 25 '11 at 10:37
  • @Ahmed, probably I am missing something but the other answer that you accepted doesn't work for extracting subdomain from full domain name, eg: `preg_match('/^[a-z][a-z-]{5,19}$/', "foo.example.com", $m ); var_dump($m);` – anubhava Apr 25 '11 at 11:56