2016-05-10 29 views
0

我正在構建一個用戶評論和獲得信用的系統。快速得到信用戶添加評論,如「fffff」,「niceeeeeeeeee」,「greeeeeeaaaatt」,「aaaa」,「b」等...
有反正過濾掉這些評論。任何建議將不勝感激。在php中過濾適當的詞語和正確的英語語言

+0

建立您的語言詞典並檢查它之前的文字作爲評論發佈之前。 – Thamilan

+0

編寫一個表達式,在一行中查找3個相同的字母並搜索輸入,然後拒絕所有無效的表達式。 –

回答

0

你可以檢查是否用戶的輸入包含使用正則表達式(因爲我不知道在英語具有相同字母的3行中的任意字)

3個連續的字符
$user_input = "niceeeeeeeeeeee"; 

if (preg_match("/([A-Za-z])\\1\\1/", $user_input)) { 
    echo "String contains the same letter 3 times in a row and is not valid"; 
} else { 
    echo "String is ok!"; 
} 

這會匹配「niceee」,「greeeat」,「aaaa」等,或連續3次或更多次使用相同字母的任何字符串。如果要檢查用戶的輸入對多個模式,你可以把你的正則表達式中的數組,並檢查他們都如:

$patterns = [ 
    "/(.)\\1\\1/",   // any character (not just letters) 3+ times in a row 
    "/^.$/",     // a single character 
    "/.{15,}/",    // contains a word longer than 15 characters 
    "/([A-Za-z]{2,})\\1\\1/" // 2 letters alternating e.g. "abababab" 
]; 

foreach($patterns as $pattern){ 
    if (preg_match($pattern, $user_input)) { 
     echo "This is an invalid string"; 
    } 
} 

或者,如果你沒有太多的模式(而你不是與可讀性有關),您可以將所有模式連同|連接起來。

if (preg_match("/(.)\\1\\1|^.$|.{15,}|([A-Za-z]{2,})\\2\\2/", $user_input)) { 
    echo "This is an invalid string"; 
} 
+0

好的建議..這個怎麼樣..「abababababa」:) – user3516704

+0

沒有正則表達式可以捕捉各種亂碼,但你可以選擇你想過濾的特定模式。我解決了你在問題中提出的問題,但是「正常」的正則表達式可能是「/([A-Za-z] {2,})\\ 1 \\ 1 /」'。我推薦一個像這樣的工具來玩你的正則表達式:[link](https://regex101.com/)。 – ahaurat

+0

很好..謝謝:) – user3516704

0

爲了測試正確的拼寫,您可以使用pspell_check()函數。

$pspell_link = pspell_new("en"); 

if (pspell_check($pspell_link, "niceeeeeeeeee")) { 
    echo "Correct spelling."; 
} else { 
    echo "Wrong spelling"; 
} 
相關問題