密码字母数字PHP不使用正则表达式


password alphanumeric php without use regex

我就这样试试。

for($i=0;$i<$length;i++)
{
    if(!ctype_alpha($password[$i])
    {
        header("location:regist.php?err=Password not contain letter");
    }
    else if(!is_numeric($password[$i]))
    {
        header("location:regist.php?err=Password not contain  numbers");
    }
    else
    {
        //??
    }
}

如果密码必须是字母数字而不使用正则表达式,如何验证密码?

我想你是在找ctype_alnum检查——http://www.php.net/manual/en/function.ctype-alnum.php

更新——

您可以使用-

来检查字符串中的特殊字符
<?php
$test = "password123";
$special_char = "#$%^&*()+=-[]';,./{}|:<>?~";
if (false === strpbrk($test, $special_char))
{
    echo "Good Password";
}
?>

(可能这是丑陋的编码),但这是你需要的

$str="123adsd##$";
$error ='string should be alphanumeric';
$str1 = str_replace(range(0,9), '', $str);
$alph = array_merge(range('A', 'Z'), range('a', 'z'));
if(strlen($str1)>strlen($str) || strlen($str1)<strlen($str))
{
    $str2 = str_replace($alph, '', $str1);
if(strlen($str2)>strlen($str1) || strlen($str2)<strlen($str1))
{
  if(strlen($str2) >0)
  {
    echo $error;
  }
}
else{
    echo $error;
}
}
else{
    echo $error;
}
输出

string should be alphanumeric