首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >php regex ctype

php regex ctype
EN

Stack Overflow用户
提问于 2012-01-20 05:30:47
回答 3查看 210关注 0票数 1

我需要一个正则表达式来查看$input是否只包含字母字符或空格,也需要一个正则表达式来检查$numInput是否只包含数字字符或空格,还有一个组合如下:

代码语言:javascript
复制
$alphabeticOnly = 'abcd adb';
$numericOnly = '1234 567';
$alphabeticNumeric = 'abcd 3232';

因此,在上面的所有示例中,字母、数字和空格都不允许使用符号。

怎样才能得到这3个不同的正则表达式?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-01-20 05:37:43

这应该会对你有帮助

代码语言:javascript
复制
if (!preg_match('/^[\sa-zA-Z]+$/', $alphabeticOnly){
    die('alpha match fail!');
}

if (!preg_match('/^[\s0-9]+$/', $numericOnly){
    die('numeric match fail!');
}

if (!preg_match('/^[\sa-zA-Z0-9]+$/', $alphabeticNumeric){
    die('alphanumeric match fail!');
}
票数 2
EN

Stack Overflow用户

发布于 2012-01-20 05:38:20

这是非常基础的

代码语言:javascript
复制
/^[a-z\s]+$/i - letter and spaces
/^[\d\s]+$/ - number and spaces
/^[a-z\d\s]+$/i - letter, number and spaces

只需在preg_match()中使用它们

票数 2
EN

Stack Overflow用户

发布于 2012-01-20 18:31:18

为了兼容unicode,您应该使用:

代码语言:javascript
复制
/^[\pL\s]+$/      // Letters or spaces
/^[\pN\s]+$/      // Numbers or spaces
/^[\pL\pN\s]+$/   // Letters, numbers or spaces
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8933560

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档