用于检查字符串是否包含数据库中的单词的函数

Lee*_*ice 2 php mysql

我有一个表,其中包含一个被阻止的单词列表

+--------+------------------+
|  id    |  word            |
+--------+------------------+
|   1    |  this            |
|   2    |  word            |
|   3    |  is              |
|   4    |  blocked         |
+--------+------------------+
Run Code Online (Sandbox Code Playgroud)

我需要做的是编写一个函数来检查字符串是否包含一个被阻止的单词(表中的单词),例如:

function blocked($string){
    if(***blocked***){
        return true;
    }else{
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

true无论单词是单独的单词,还是隐藏在另一个单词中,该函数都应该返回:

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


$string = "iwanttoseeifthisstringisblocked"; //returns `true`
Run Code Online (Sandbox Code Playgroud)

希望我的问题很明确,

任何帮助赞赏

小智 5

您可以使用此或类似的数据库查询:

select word from blocked_list where "you string" like concat("%", word, "%")
Run Code Online (Sandbox Code Playgroud)