Nic*_*sta 19
http://php.net/manual/en/function.ctype-alpha.php
<?php
$ch = 'a';
if (ctype_alpha($ch)) {
// Accept
} else {
// Reject
}
Run Code Online (Sandbox Code Playgroud)
如果正确设置,这也会考虑区域设置.
编辑:要完成,其他海报似乎认为您需要确保参数是单个字符,否则参数无效.要检查字符串的长度,可以使用strlen().如果strlen()返回任何非1号,那么您也可以拒绝该参数.
就目前而言,您回答时的问题表明您在某处有一个字符参数,并且您想要检查它是否按字母顺序排列.我提供了一个通用的解决方案来做到这一点,并且也是对语言环境友好的.
Asa*_*aph 14
在方法的顶部使用以下保护条款:
if (!preg_match("/^[a-z]$/", $param)) {
// throw an Exception...
}
Run Code Online (Sandbox Code Playgroud)
如果您也想允许大写字母,请相应地更改正则表达式:
if (!preg_match("/^[a-zA-Z]$/", $param)) {
// throw an Exception...
}
Run Code Online (Sandbox Code Playgroud)
支持不区分大小写的另一种方法是使用/i大小写不敏感修饰符:
if (!preg_match("/^[a-z]$/i", $param)) {
// throw an Exception...
}
Run Code Online (Sandbox Code Playgroud)