PHP Detect Duplicate Text

后端 未结 9 1352
夕颜
夕颜 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:23

    Another idea would be to use substr_count iteration:

    $str = "Love a and peace love a and peace love a and peace love a and peace love a and peace love a and peace";
    
    $rep = "";
    
    $str = strtolower($str);
    for($i=0,$len=strlen($str),$pattern=""; $i<$len; ++$i) {
      $pattern.= $str[$i];
      if(substr_count($str,$pattern)>1)
        $rep = strlen($rep)strlen($str)/5) echo "Repetitive string alert!";
    else echo "String seems to be non-repetitive.";
    
    echo " Longest pattern found: '$rep'";
    

    Which would output

    Repetitive string alert! Longest pattern found: 'love a and peace love a and peace love a and peace'
    

提交回复
热议问题