How to create a regular expression?

Hello,

I am trying to create a simple regular expression but i am not getting it. I want a simple regular expression which should check if the user has entred 10 chars. whcih are all numbers.

I tried the following but i don’t understand why it is not working:

if (preg_match ("(?\\b[0-9]{10}\\)\\b", "1234567890")) { echo "match"; } else { echo "no match"; }

Please help.

Thanks.

You could look at regular-expression.info for good explanations on regex.

As for your code:


if (preg_match ("/^\\d{10}$/", "1234567890")) {
  echo "match";
} else {
  echo "no match";
}

/ - start regex
^ - match start of string - i.e., don’t match if there is anything before here
\d - match all digit (0-9)
{10} - match exactly 10 times
$ - match end of string - i.e., don’t match if there is anything after here
/ - end of regex