PHP Detect Duplicate Text

后端 未结 9 1351
夕颜
夕颜 2021-02-05 07:52

I have a site where users can put in a description about themselves.

Most users write something appropriate but some just copy/paste the same text a number of times (to

9条回答
  •  孤城傲影
    2021-02-05 08:38

    You could use a regex, like this:

    if (preg_match('/(.{10,})\\1{2,}/', $theText)) {
        echo "The string is repeated.";
    }
    

    Explanation:

    • (.{10,}) looks for and captures a string that is at least 10 characters long
    • \\1{2,} looks for the first string at least 2 more times

    Possible tweaks to suit your needs:

    • Change 10 to a higher or lower number to match longer or shorter repeated strings. I just used 10 as an example.
    • If you want to catch even one repetition (love and peace love and peace), delete the {2,}. If you want to catch a higher number of repetitions, increase the 2.
    • If you don't care how many times the repetition occurs, only that it occurs, delete the , in {2,}.

提交回复
热议问题