php 如何检查字符是字母还是数字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14230986/
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
How can I check if a char is a letter or a number?
提问by gadss
I have a string and I want to loop it so that I can check if every char is a letter or number.
我有一个字符串,我想循环它,以便我可以检查每个字符是字母还是数字。
$s = "rfewr545 345b";
for ($i=1; $i<=strlen($s); $i++){
if ($a[$i-1] == is a letter){
echo $a[$i-1]." is a letter";
} else {
echo $a[$i-1]." is a number";
}
}
How can I check if a char is a letter or a number?
如何检查字符是字母还是数字?
回答by hsz
To test if character is_numeric, use:
要测试 character is_numeric,请使用:
is_numeric($a[$i-1])
As below:
如下:
$s = "rfewr545 345b";
for ($i = 0; $i < strlen($s); $i++){
$char = $s[$i];
if (is_numeric($char)) {
echo $char . ' is a number';
} else {
echo $char . ' is a letter';
}
}
回答by codaddict
回答by Boaz - Reinstate Monica
With regular expressions you can try the following.
使用正则表达式,您可以尝试以下操作。
Test for a number
测试一个数字
if (preg_match('/\d/', $char)) :
echo $char.' is a number';
endif;
Test for a "letter"
测试“字母”
if (preg_match('/[a-zA-Z]/', $char)) :
echo $char.' is a letter';
endif;
The benefit of this approach is mainly from the "letter" test, which lets you efficiently define what constitutesas a "letter" character. In this example, the basic English alphabet is defined as a "letter".
这种方法的好处主要来自“字母”测试,它可以让您有效地定义什么构成“字母”字符。在这个例子中,基本的英文字母被定义为一个“字母”。
回答by Sumit Malik
php provide some nice function to checkout chars. Use the apt functions as conditions in if blocks.
php 提供了一些很好的函数来检查字符。使用 apt 函数作为 if 块中的条件。
visit:
访问:
e.g. ctype_digitreturns true if number is numeric.
例如,如果 number 是数字,则ctype_digit返回 true。
回答by Ani Menon
You may use ctype_alphato check for alphabetic character(s).
您可以使用ctype_alpha来检查字母字符。
Similarly, you may use ctype_digitto check for numeric character(s).
同样,您可以使用ctype_digit来检查数字字符。
is_numeric— Finds whether a variable is a number or a numeric string
is_numeric— 查找变量是数字还是数字字符串
is_numeric()example:
is_numeric()例子:
<?php
$tests = array(
"42",
0b10100111001,
"not numeric",
array(),
9.1
);
foreach ($tests as $element) {
if (is_numeric($element)) {
echo "'{$element}' is numeric", PHP_EOL;
} else {
echo "'{$element}' is NOT numeric", PHP_EOL;
}
}
?>
The above example will output:
上面的例子将输出:
'42' is numeric
'1337' is numeric
'not numeric' is NOT numeric
'Array' is NOT numeric
'9.1' is numeric
Where ctype_digit()and is_numeric()differ?
哪里ctype_digit()又is_numeric()不同?
Example comparing strings with integers:
比较字符串与整数的示例:
<?php
$numeric_string = '42';
$integer = 42;
ctype_digit($numeric_string); // true
ctype_digit($integer); // false (ASCII 42 is the * character)
is_numeric($numeric_string); // true
is_numeric($integer); // true
?>
回答by Tikkes
See this: http://php.net/manual/en/function.is-numeric.php
看到这个:http: //php.net/manual/en/function.is-numeric.php
if(Is_numeric($char)) {
//Do stuff
}
else {
//Do other stuff
}
回答by Edwin Alex
You can do by using is_numeric()function
您可以使用is_numeric()函数
if (is_numeric($a[$i-1])){
echo $a[$i-1]." is a number";
} else {
echo $a[$i-1]." is a letter";
}

