我需要一个正则表达式来查看$input是否只包含字母字符或空格,也需要一个正则表达式来检查$numInput是否只包含数字字符或空格,还有一个组合如下:
$alphabeticOnly = 'abcd adb';
$numericOnly = '1234 567';
$alphabeticNumeric = 'abcd 3232';因此,在上面的所有示例中,字母、数字和空格都不允许使用符号。
怎样才能得到这3个不同的正则表达式?
发布于 2012-01-20 05:37:43
这应该会对你有帮助
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!');
}发布于 2012-01-20 05:38:20
这是非常基础的
/^[a-z\s]+$/i - letter and spaces
/^[\d\s]+$/ - number and spaces
/^[a-z\d\s]+$/i - letter, number and spaces只需在preg_match()中使用它们
发布于 2012-01-20 18:31:18
为了兼容unicode,您应该使用:
/^[\pL\s]+$/ // Letters or spaces
/^[\pN\s]+$/ // Numbers or spaces
/^[\pL\pN\s]+$/ // Letters, numbers or spaceshttps://stackoverflow.com/questions/8933560
复制相似问题