php 判断一个字符是否是字母
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2227080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Determine if a character is alphabetic
提问by Ed.
Having problems with this.
有这方面的问题。
Let's say I have a parameter composed of a single character and I only want to accept alphabetic characters. How will I determine that the parameter passed is a member of the latin alphabet (a–z)?
假设我有一个由单个字符组成的参数,我只想接受字母字符。我将如何确定传递的参数是拉丁字母 ( a– z) 的成员?
By the way Im using PHP Kohana 3.
顺便说一句,我使用 PHP Kohana 3。
Thanks.
谢谢。
回答by Nick Presta
http://php.net/manual/en/function.ctype-alpha.php
http://php.net/manual/en/function.ctype-alpha.php
<?php
$ch = 'a';
if (ctype_alpha($ch)) {
// Accept
} else {
// Reject
}
This also takes locale into account if you set it correctly.
如果您正确设置,这也会考虑区域设置。
EDIT:To be complete, other posters here seem to think that you need to ensure the parameter is a single character, or else the parameter is invalid. To check the length of a string, you can use strlen(). If strlen()returns any non-1 number, then you can reject the parameter, too.
编辑:为了完整起见,这里的其他海报似乎认为您需要确保参数是单个字符,否则参数无效。要检查字符串的长度,您可以使用strlen()。如果strlen()返回任何非 1 数字,那么您也可以拒绝该参数。
As it stands, your question at the time of answering, conveys that you have a single character parameter somewhere and you want to check that it is alphabetical. I have provided a general purpose solution that does this, and is locale friendly too.
就目前而言,您在回答时的问题表明您在某处有一个单个字符参数,并且您想检查它是否按字母顺序排列。我提供了一个通用的解决方案,可以做到这一点,并且对语言环境也很友好。
回答by Asaph
Use the following guard clause at the top of your method:
在方法的顶部使用以下保护子句:
if (!preg_match("/^[a-z]$/", $param)) {
// throw an Exception...
}
If you want to allow upper case letters too, change the regular expression accordingly:
如果您也想允许大写字母,请相应地更改正则表达式:
if (!preg_match("/^[a-zA-Z]$/", $param)) {
// throw an Exception...
}
Another way to support case insensitivity is to use the /icase insensitivity modifier:
另一种支持不区分大小写的方法是使用/i不区分大小写的修饰符:
if (!preg_match("/^[a-z]$/i", $param)) {
// throw an Exception...
}
回答by Ruchir Shah
preg_match('/^[a-zA-Z]$/', $var_vhar);
Method will return int value: for no match returns 0 and for matches returns 1.
方法将返回 int 值:不匹配返回 0,匹配返回 1。
回答by Ahmad
You can't use [a-zA-Z] for Unicode.
您不能将 [a-zA-Z] 用于 Unicode。
here are the example working with Unicode,
这是使用 Unicode 的示例,
if ( preg_match('/^\p{L}+$/u', 'my text') ) {
echo 'match';
} else {
echo 'not match';
}
回答by Khaled Mohammad
This will help hopefully.This a simple function in php called ctype_alpha
这将有希望。这是一个简单的 php 函数,名为ctype_alpha
$mystring = 'a'
if (ctype_alpha($mystring))
{
//Then do the code here.
}
回答by Anthony
I'd use ctype, as Nick suggested,since it is not only faster than regex, it is even faster than most of the string functions built into PHP. But you also need to make sure it is a single character:
正如尼克建议的那样,我会使用 ctype,因为它不仅比 regex 快,而且比 PHP 中内置的大多数字符串函数还要快。但您还需要确保它是单个字符:
if (ctype_alpha($ch) && strlen($ch) == 1) {
// Accept
} else {
// Reject
}
回答by codaddict
You can try:
你可以试试:
preg_match('/^[a-zA-Z]$/',$input_char);
The return value of the above function is true if the $input_char contains a single alphabet, else it is false. You can suitably make use of return value.
如果 $input_char 包含单个字母,则上述函数的返回值为真,否则为假。您可以适当地利用返回值。

