提供三種方法來判斷一個字符串中是否包括我們定義好的詞,這比較適合于在留言,評論等地址進(jìn)行關(guān)鍵詞過濾,實(shí)例代碼如下:
- $crud = array('中國|||我國|||大地', 'kelon|||lerke|||sb', 'sesscxx');
- $crud = join('|',$crud);
- $crud = str_replace('|||', '|', $crud);
- $pat = "/({$crud})/i";
- $txt = '我知道中國你是sdfex誰??!';
- preg_match/*_all*/($pat, $txt, $matches);
- var_dump($matches);
方法二,代碼如下:
- function checkcrud($str, $crud)
- {
- if(is_array($crud) && !emptyempty($crud))
- {
- foreach($crud as $value)
- {
- if(strpos($value, '|||') !== false)
- {
- $cruds = explode('|||', $value);
- $num = count($cruds);
- $check = 0;
- foreach($cruds as $val)
- {
- if(strpos($str, $val) !== false)
- {
- $check++;
- }
- }
- if($check == $num)
- {
- return true;
- }
- }
- else
- {
- if(strpos($str, $value) !== false)
- {
- return true;
- }
- }
- }
- return false;
- }
- }
- $crud = array('中國|||我國|||大地', 'kelon|||lerke|||ssxb', 'aaa');
- $test1 = '我是中國人.我國人很多.大地上全是人.-__-!!好xx的造句.';
- var_dump(checkcrud($test1, $crud));
方法三,代碼如下:
轉(zhuǎn)載請注明來源:php數(shù)組查找關(guān)鍵函數(shù)
- function lktest($v,$keyword){
- foreach ($v as $k){
- if (strpos($k,"|||")!==false){
- $kelon=explode("|||",$k);
- //求數(shù)組最大值
- $b=count($kelon);
- foreach($kelon as $t){
- //echo $t.'<br>';
- if (preg_match('/'.$t.'/i',$keyword)){
- //echo "敏感關(guān)鍵字";
- $a=$a+1;
- }
- else{
- $a='';
- }
- }
- //如果數(shù)組最大值跟$a相等時,則所有|||關(guān)鍵字都出現(xiàn)了
- if ($a==$b){
- echo "敏感關(guān)鍵字";
- }
- }
- elseif(preg_match('/'.$k.'/i',$keyword)){
- echo "敏感關(guān)鍵字";//開源代碼phpfensi.com
- }
- }
- }
哈爾濱品用軟件有限公司致力于為哈爾濱的中小企業(yè)制作大氣、美觀的優(yōu)秀網(wǎng)站,并且能夠搭建符合百度排名規(guī)范的網(wǎng)站基底,使您的網(wǎng)站無需額外費(fèi)用,即可穩(wěn)步提升排名至首頁。歡迎體驗(yàn)最佳的哈爾濱網(wǎng)站建設(shè)。
