2011-10-13 65 views
2

我有一個包含受阻話功能如果字符串包含從數據庫中字

+--------+------------------+ 
| id | word   | 
+--------+------------------+ 
| 1 | this   | 
| 2 | word   | 
| 3 | is    | 
| 4 | blocked   | 
+--------+------------------+ 

我需要做的是,如果一個字符串包含阻塞寫檢查的功能列表的表字(表中的字)例如:

function blocked($string){ 
    if(***blocked***){ 
     return true; 
    }else{ 
     return false; 
    } 
} 

函數應該返回true不管這個詞本身就是一個字,或者如果它隱藏在另一個詞:

$string = "I want to see if this string is blocked"; //returns `true` because it contains 'this', 'is' and 'blocked' 


$string = "iwanttoseeifthisstringisblocked"; //returns `true` 

希望我的問題是清楚的,

任何幫助表示讚賞

+0

構建從表或谷歌博耶 - 穆爾一個高性能正則表達式 – sehe

回答

5

您可以使用此或類似的數據庫查詢:

select word from blocked_list where "you string" like concat("%", word, "%") 
+0

是什麼'concat'是什麼意思? –

+0

CONCAT - 連接字符串的函數http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat – Alex

1

獲取表的內容到一個數組,然後 - >

$comment = "iwanttoseeifthisstringisblocked"; 
$words = array('this','word','is','blocked'); 

function is_array_in_string($comment, $words) 
{ 
    foreach ($words as $item) 
    { 
     if (strpos($comment, $item) !== false) 
      return true; 
    } 
    return false; 
} 
0
while ($row = mysql_fetch_array($res)){ 
    if (!(strpos($your_string, $row['word']) === false)){ 
     return true; 
    }; 
    return false; 
} 

其他e $res是執行的結果SELECT word FROM words

0

如果您希望在PHP中完成,您需要使用strstr();函數。

但你可以做到這一點在你的MySQL使用LIKE%查詢