正则表达式检查以空格和总长度开头的字符串10 PHP

I wrote PHP code to check the string with regular expression. Every string should start with white space and total length should be exactly 10.

$str = ' 123456789';
if(preg_match('/^([ 0-9]){10}+$/', $str)){
      echo 'true';
}
    echo 'false';

I have expected the following results when I change the $str variable. But using the above regular expression, I only get the first one right.

$str = ' 1234567890'; //true
$str = '1234567890'; // false 

If you truly mean white space then use \s as your white space because it grabs tabs as well. Also, It looks like you only want digits. If so use:

'/^\s\d{9}$/'

If not then use:

'/^\s.{9}$/'

You are almost there but in your regex you are looking for 10 chars from set of spaces and digits.

That's not exactly what you wanted (space is allowed only as first char).

^ [0-9]{10}$

As you can see I removed + from the end and also space from chars set. Instead I put it on the beginning of the expression.

Here is the regex

^ \d{9}$

that checks if string
+ starts with space character (qty=1)
+ contains digit in qty=9

that make total 10 characters

DEMO