1 回答

TA貢獻1831條經驗 獲得超9個贊
您可以這樣做:
刪除所有可能影響將字符串分解為單個單詞的標點符號等,并通過將所有非字母數字字符替換為空格來確保單詞由空格分隔,例如一、二、三現在可以識別為 3 個單獨的單詞)
將輸入字符串和臟話字符串轉換為小寫以便于比較
將兩個字符串分解為數組(這是替換輸入字符串中的空格很重要的地方!)
交叉數組以找到兩者共有的單詞
您可能還想考慮從輸入字符串中刪除數字,具體取決于您想要如何處理數字。
完整代碼及詳細注釋如下:
// 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 關注
- 126 瀏覽
添加回答
舉報