0

当有字数要求时。一些用户可能会用重复的垃圾文本填充剩余的配额。我想设置允许的重复次数。

4

1 回答 1

1
    $input = "this is a test to see if there are repeats in this sentence blah blah blah as these three here";


    if(check_input($input, 3) === false)
        var_dump("No repeat found");
    else
        var_dump("Repeat found");


    function check_input($input, $maximum_repeats){
        $repeat_found = false;

        for($i = 0; $i < strlen($input) && $repeat_found == false; $i++){

            for($k = 0; $k < strlen($input) && $repeat_found == false; $k++){
                $test = substr($input, $i, $k);

                if(strlen($test) > 0){
                    if(find_repeat($test, $input, $maximum_repeats) === true){
                        $repeat_found = true;
                    }
                }
            }
        }

        return $repeat_found;
    }

    function find_repeat($target, $input, $amount){
        $needle = "";

        for($i = 0; $i < $amount; $i++)
            $needle .= $target;

        return strpos($input, $needle) !== false;
    }

这个函数对于“blah blah blah”和其他例子来说都是正确的,例如

好的...

于 2012-07-06T15:21:34.867 回答