Check if string contains a word then perform action

时间:2022-09-08 01:44:26

Currently I have a simple swear filter in place with an array of words, I am now also wanting to do checking on strings to see if they contain any:

目前我有一个简单的脏话过滤器,有一个单词数组,我现在也想检查字符串,看看它们是否包含任何:

Here's my list:

这是我的清单:

$words = array(
"/foo/is",
"/bar/is",
"/baz/is"
);

So I basically want to search a string for any of those words, if it picks up even one of them, then do something.

所以我基本上想要搜索一个字符串中的任何一个单词,如果它甚至拿起其中一个,那么就做一些事情。

Is that easily doable?

那容易行吗?

1 个解决方案

#1


2  

did you mean something like:

你的意思是:

$words = array(
    "/foo/is",
    "/bar/is",
    "/baz/is"
);

$myWord = 'hello';

foreach($words as $check) {
   if (preg_match($check, $myWord )) {
       // do something
   }
}

#1


2  

did you mean something like:

你的意思是:

$words = array(
    "/foo/is",
    "/bar/is",
    "/baz/is"
);

$myWord = 'hello';

foreach($words as $check) {
   if (preg_match($check, $myWord )) {
       // do something
   }
}