我有一个表,其中包含阻止字的列表
+--------+------------------+
| 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`
希望我的问题很清楚,
感谢的任何帮助
您可以使用以下或类似的数据库查询:
select word from blocked_list where "you string" like concat("%", word, "%")
将表的内容放入数组中,然后->
$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;
}
while ( $row = mysql_fetch_array($res) ){
if ( !( strpos($your_string, $row['word']) === false ) ){
return true;
};
return false;
}
其中$res
是执行SELECT word FROM words
的结果
如果你想在PHP中完成,你需要使用strstr();
函数。
但是您可以在MySQL查询
LIKE
和%
来完成此操作