[Comparison] which of the following is better if PHP checks whether the submitted paragraphs have repeated rows? Solution

Source: Internet
Author: User
[Comparison] which of the following is better if PHP checks whether the submitted paragraphs have repeated rows? I wrote two functions, compared to the submitted text section, and found some problems: (1) in_array () I found that there are some problems in Chinese, but I was prompted that there was no such problem, the probability of long text is higher (2) Sometimes the short paragraph repeats 3 ~ 4 [comparison] which of the following is better if PHP checks whether the submitted paragraphs have repeated rows?
The two functions are compared to the submitted text paragraphs, and some problems are found:
(1) the in_array () method is used to detect Chinese characters, but the system prompts that the text does not exist. the probability of long text is higher.
(2) Sometimes the short paragraph repeats 3 ~ It is allowed for four times, but if similar_text is used for comparison, the user will be rejected if there is a duplicate. How to improve
(3) Is there any better method ~





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;
}


/* Duplicate paragraph detection */
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 times
} Elseif ($ length> 12 & $ length <51 ){
$ CountMiddle ++;
If ($ countMiddle> 3) return true; // 4 times
} Elseif ($ length> 50 & $ length <101 ){
$ CountLong ++;
If ($ countLong> 2) return true; // 3 times

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.