在字符串preg_match数组项? [英] preg_match array items in string?
本文介绍了在字符串preg_match数组项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可以说我有不好的话数组:
Lets say I have an array of bad words:
$badwords = array("one", "two", "three");
和随机字符串:
$string = "some variable text";
如何建立这个循环:
How to create this cycle:
if (one or more items from the $badwords array is found in $string)
echo "sorry bad word found";
else
echo "string contains no bad words";
例:
如果 $字符串=一个晴朗的一天或某一天我们两个做了
,用户应该看到抱歉不好的词发现的消息。
如果 $字符串=青天白日
,用户应该看到的字符串中不包含脏话的消息。
Example:
if $string = "one fine day" or "one fine day two of us did something"
, user should see sorry bad word found message.
If $string = "fine day"
, user should see string contains no bad words message.
据我所知,你不能 preg_match
的数组。有何意见?
As I know, you can't preg_match
from array. Any advices?
推荐答案
这个怎么样:
$badWords = array('one', 'two', 'three');
$stringToCheck = 'some stringy thing';
// $stringToCheck = 'one stringy thing';
$noBadWordsFound = true;
foreach ($badWords as $badWord) {
if (preg_match("/\b$badWord\b/", $stringToCheck)) {
$noBadWordsFound = false;
break;
}
}
if ($noBadWordsFound) { ... } else { ... }
这篇关于在字符串preg_match数组项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文