第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何從 PHP 字符串中的數(shù)組中查找準(zhǔn)確的單詞

如何從 PHP 字符串中的數(shù)組中查找準(zhǔn)確的單詞

PHP
慕后森 2023-11-03 15:27:33
我正在搜索stringan 中的一組單詞,array以通知用戶是否找到任何單詞。但是,我得到的結(jié)果并不完全匹配。關(guān)于如何讓它只顯示完全匹配的任何想法。我的代碼如下所示。<?php// Profanity check   $profaneReport = ""; $allContent = "Rice Beans Class stite"; $profanity_list = "lass tite able";      $profaneWords = explode( ' ', $profanity_list );      $wordsFoundInProfaneList = []; // Create words an array      //search for the words;      foreach ( $profaneWords as $profane ) {        if ( stripos( $allContent, $profane ) !== false ) {          $wordsFoundInProfaneList[ $profane ] = true;        }      }    // check if bad words were found      if ( $wordsFoundInProfaneList !== 0 ) {         $profaneReportDesc = "Sorry, your content may contain such words as " . "<strong>" . implode( ", ", array_keys( $wordsFoundInProfaneList )) . '</strong>"';      } else {       $profaneReportDesc = "Good: No profanity was found in your content";      }     echo $profaneReportDesc;  ?>上面的代碼返回“抱歉,您的內(nèi)容可能包含諸如 lass, tite”這樣的單詞,當(dāng)它們與中的單詞不完全匹配時(shí)$allContent
查看完整描述

1 回答

?
天涯盡頭無(wú)女友

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超9個(gè)贊

您可以這樣做:

  1. 刪除所有可能影響將字符串分解為單個(gè)單詞的標(biāo)點(diǎn)符號(hào)等,并通過(guò)將所有非字母數(shù)字字符替換為空格來(lái)確保單詞由空格分隔,例如一、二、三現(xiàn)在可以識(shí)別為 3 個(gè)單獨(dú)的單詞)

  2. 將輸入字符串和臟話字符串轉(zhuǎn)換為小寫以便于比較

  3. 將兩個(gè)字符串分解為數(shù)組(這是替換輸入字符串中的空格很重要的地方!)

  4. 交叉數(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;


查看完整回答
反對(duì) 回復(fù) 2023-11-03
  • 1 回答
  • 0 關(guān)注
  • 136 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)