你可以使用我前一段时间做的这个函数来输入密码。您可以通过修改 if 条件将其用于任何字符串。把每个特殊字符用\放在前面。它还检查字符串是否为 8-20 个字符长
    function isPasswordValid($password){
            $whiteListed = "\$\@\#\^\|\!\~\=\+\-\_\.";
            $status = false;
            $message = "Password is invalid";
            $containsLetter  = preg_match('/[a-zA-Z]/', $password);
            $containsDigit   = preg_match('/\d/', $password);
            $containsSpecial = preg_match('/['.$whiteListed.']/', $password);
            $containsAnyOther = preg_match('/[^A-Za-z-\d'.$whiteListed.']/', $password);
            if (strlen($password) < 8 ) $message = "Password should be at least 8 characters long";
            else if (strlen($password) > 20 ) $message = "Password should be at maximum 20 characters long";
            else if(!$containsLetter) $message = "Password should contain at least one letter.";
            else if(!$containsDigit) $message = "Password should contain at least one number.";
            else if(!$containsSpecial) $message = "Password should contain at least one of these ".stripslashes( $whiteListed )." ";
            else if($containsAnyOther) $message = "Password should contain only the mentioned characters";
            else {
                $status = true;
                $message = "Password is valid";
            }
            return array(
                "status" => $status,
                "message" => $message
            );
    }
输出
$password = "asdasdasd"
print_r(isPasswordValid($password));
// [
//   "status"=>false,
//   "message" => "Password should contain at least one number."
//]
$password = "asdasd1$asd"
print_r(isPasswordValid($password));
// [
//   "status"=>true,
//   "message" => "Password is valid."
//]