[英]Check if a string is alphanumeric without regular expressions
I want to check if a string is alpha-numeric and don't intend to use Regex
. 我想检查一个字符串是否为字母数字,并且不打算使用
Regex
。 I have am getting the correct answer but somehow the program is throwing an error stating undefined offset
. 我得到了正确的答案,但是不知何故该程序抛出了一个错误,指出
undefined offset
。 I have checked the array keys and seemingly they are fine. 我检查了阵列键,看来它们很好。
$str="hello";
$arr=str_split($str);//convert a string to an array
$a=0;
$d=0;
for($i=0;$i<=count($arr);$i++)
{
if($arr[$i]>='a' && $arr[$i]<='z' || $arr[$i]>='A' && $arr[$i]<='Z')
{
$a=1;
}
elseif($arr[$i]>='0' && $arr[$i]<='9')
{
$d=1;
}
}
if($a==1&&$d==1)
{
echo "Alphanumeric";
}
else
{
echo "Not alphanumeric";
}
数组从索引零开始,所以结束ist i<count
for($i=0;$i<count($arr);$i++)
You should check out the ctype_alnum ( string $text )
; 您应该检出
ctype_alnum ( string $text )
; function. 功能。
$str="hello";
if(ctype_alnum($str))
{
echo "Alphanumeric";
}
else
{
echo "Not alphanumeric";
}
您应该使用$i < count($arr)
或$i <= count($arr) -1
因为数组从0开始,只有$i <= count($arr)
会导致undefined offset
错误消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.