匹配数字和空格的正则表达式 [英] regular expression for matching number and spaces

查看:70
本文介绍了匹配数字和空格的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个只允许空格和数字的正则表达式..

i want to create a regular expression which will allow only spaces and number..

我如何用 PHP 来检查这个?

And how can i check this with PHP?

推荐答案

$pattern = '/^[0-9 ]+$/';

if ( preg_match ($pattern, $text) )
{
    echo 'allowed';
}

如果您想限制为 15 个字符(如您在评论中提到的),您可以使用 { } 来分隔最小和最大长度.

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 = '/^[0-9 ]{1,15}$/';

允许 1 到 15 个字符.

to allow 1 to 15 chars.

这篇关于匹配数字和空格的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆