1 回答

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超9個(gè)贊
您可以這樣做:
刪除所有可能影響將字符串分解為單個(gè)單詞的標(biāo)點(diǎn)符號(hào)等,并通過(guò)將所有非字母數(shù)字字符替換為空格來(lái)確保單詞由空格分隔,例如一、二、三現(xiàn)在可以識(shí)別為 3 個(gè)單獨(dú)的單詞)
將輸入字符串和臟話字符串轉(zhuǎn)換為小寫以便于比較
將兩個(gè)字符串分解為數(shù)組(這是替換輸入字符串中的空格很重要的地方!)
交叉數(shù)組以找到兩者共有的單詞
您可能還想考慮從輸入字符串中刪除數(shù)字,具體取決于您想要如何處理數(shù)字。
完整代碼及詳細(xì)注釋如下:
// Profanity check??
$profaneReport = "";
$profanity_list = "hello TEN test commas";? ??
$allContent = "Hello, world! This is a senTENce for testing. It has more than TEN words and contains some punctuation,like commas.";
/* Create an array of all words in lowercase (for easier comparison) */
$profaneWords = explode( ' ', strtolower($profanity_list) );
/* Remove everything but a-z (i.e. all punctionation numbers etc.) from the sentence?
? ?We replace them with spaces, so we can break the sentence into words */
$alpha = preg_replace("/[^a-z0-9]+/", " ", strtolower($allContent));
/* Create an array of the words in the sentence */
$alphawords = explode( ' ', $alpha );
/* get all words that are in both arrays */
$wordsFoundInProfaneList = array_intersect ( $alphawords, $profaneWords);
// check if bad words were found, and display a message?
if ( !empty($wordsFoundInProfaneList)) {
? ? $profaneReportDesc = "Sorry, your content may contain such words as " . "<strong>" . implode( ", ", $wordsFoundInProfaneList) . '</strong>"';
} else {
? ? $profaneReportDesc = "Good: No profanity was found in your content";
}
echo $profaneReportDesc;
- 1 回答
- 0 關(guān)注
- 136 瀏覽
添加回答
舉報(bào)