当前位置:Gxlcms > PHP教程 > 【对比】PHP检测提交的段落是否有重复行,哪一种更好?解决办法

【对比】PHP检测提交的段落是否有重复行,哪一种更好?解决办法

时间:2021-07-01 10:21:17 帮助过:13人阅读

【对比】PHP检测提交的段落是否有重复行,哪一种更好?
写的两个函数,对比提交的文本段落里面重复的有无,发现一些问题:
(1)in_array()检测中文有的时候会有问题,明明存在却提示不存在,长文本的时候概率更高
(2)有时候短段落重复3~4次是允许的,但是如果用similar_text作对比就导致只要有一次重复就拒绝用户提交了。如何改进才更好
(3)还有没有更好的方法,求~





function hasSimilarText($string)
{
$lineArr = explode("\n",$string);
$arrStr = $arrLen = array();
foreach($lineArr as $k => $v)
{
$arrLen[] = strlen($v);
$arrStr[] = $v;
}

foreach($arrStr as $k1 => $v1)
{
foreach($arrStr as $k2 => $v2)
{
if($k1 == $k2) continue;
if($arrLen[$k2] < 30 || abs($arrLen[$k2] - $arrLen[$k1]) > 100) continue;
similar_text($v1, $v2, $pct);
if($pct > 90) return true;
}
}
return false;
}


/* 重复段落检测 */
function hasRepeatLine($string)
{
$string = str_replace(array("\t"," ","@","#","。",",",".",","),'',$string);
//$string = str_replace("\r","\n",$string);
$lineArr = explode("\n",$string);
$countShort = $countMiddle = $countLong = 0;
$arr = array();

foreach($lineArr as $lineString)
{
$length = strlen( $lineString );
if($length < 1) continue;
if(in_array($lineString,$arr))
{
if($length < 13)
{
$countShort++;
if($countShort > 4) return true;//5次
} elseif($length>12 && $length < 51) {
$countMiddle++;
if($countMiddle > 3) return true; //4次
} elseif($length>50 && $length < 101) {
$countLong++;
if($countLong > 2) return true; //3次

人气教程排行