按字母顺序排列的PHP is_numeric
我正在寻找一个函数,它将是is_numeric的字母等效函数。如果字符串中只有字母,则返回true,否则返回false。 PHP中是否存在内置函数?按字母顺序排列的PHP is_numeric
我会用preg_match。但那是因为我从来没有听说过ctype_alpha()
。
if(!preg_match("/[^a-zA-Z]/", $teststring)) {
echo "There are only letters in this string";
} else {
echo "There are non-letters in this string";
}
如果你有特殊需要,就像只有字母+逗号和空格,那么正则表达式就是你最好的选择:) – gnud 2010-02-24 01:58:28
当我输入答案时,我喜欢/讨厌它,并且我加载新答案只看到一个比我写的那个更好。 – Erik 2010-02-24 01:58:53
请勿仅使用'[a-zA-Z]'字母!使用'\ pL'。真正的字母属性更好:'\ p {Alphabetic}'。 [这是为什么。](http://stackoverflow.com/questions/4955086/sort-upper-case-just-before-lowercase-key-values-from-a-hash/4958634#4958634) – tchrist 2011-02-10 15:23:12
!is_numeric((float)$variableToBeChecked);
也 preg_match('#^[a-z]+$#i',$variableToBeChecked); // for one or more letter character
这是怎么比'^ \ p {}字母+ $'好?它看起来严重得多。 – tchrist 2011-02-10 15:23:53
对不起,它为什么看起来更糟? – gnud 2011-02-10 18:06:53