regular expression for matching number and spaces
i want to create a regular expression which will all开发者_运维问答ow only spaces and number..
And how can i check this with PHP?
$pattern = '/^[0-9 ]+$/';
if ( preg_match ($pattern, $text) )
{
echo 'allowed';
}
Edit:
If you want to limit to 15 chars (as you mentioned in a comment) you can use { } to delimit a min and a max lenght.
pattern becomes :
$pattern = '/^[0-9 ]{1,15}$/';
to allow 1 to 15 chars.
The regex is as follows.
/^[\d ]+$/i
To answer how you run it in php, I need to know the context, do you need it to run on a single line or multi line input? do you need it to run in a loop?
This should work:
^[\s\d]*$
You should probably specify if you want "zero or more" or "one or more" spaces. The above allows for an empty string to pass evaluation.
精彩评论