如何使用密码不能包含3个或更多相同字符的序列?

时间:2010-12-18 07:20:51

标签: php

这是正确的方法......

if(strtolower($pass) == '/\d{3}/')

  {
  form_error($form, t('Passwords cannot contain sequences of 3 or more of the same character.'));
  }

Plz有人帮助我......

2 个答案:

答案 0 :(得分:4)

if(preg_match('/(.)\1\1/', $pass))

答案 1 :(得分:2)

要使用正则表达式,您需要调用preg_match或类似函数。

您正在寻找的正则表达式是:

(.)\1\1

这意味着“任何字符”后跟相同的字符后跟相同的字符。 “\ d”匹配单个数字字符。 “相同字符”部分是由于使用了反向引用 - 即,正则表达式中第一个捕获(即括号内)模式所实现的完全相同的匹配。

以下是一些允许您玩游戏的代码:

function three_in_a_row($string_to_test) {
    return preg_match('/(.)\1\1/', $string_to_test);
}

$test_strings = array(
    array('abcdefg'. false),
    array('aaa', true),
    array('baaa', true),
    array('aaab', true),
);

foreach ($test_strings as $test_string_item) {
    list($test_string, $expected_result) = $test_string_item;
    $actual_result = three_in_a_row($test_string);
    if ($actual_result != $expected_result) {
        printf("Testing string '%s'.  Expected %d, got %d.\n", $test_string, $expected_result, $actual_result);
    }
}