Php 预匹配字符串中的数组项?
假设我有一系列的坏话:Php 预匹配字符串中的数组项?,php,algorithm,preg-match,string-parsing,Php,Algorithm,Preg Match,String Parsing,假设我有一系列的坏话: $badwords = array("one", "two", "three"); 和随机字符串: $string = "some variable text"; 如何创建此循环: 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"; 示例: 如
$badwords = array("one", "two", "three");
和随机字符串:
$string = "some variable text";
如何创建此循环:
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";
示例:如果
$string=“一个晴朗的日子”或“一个晴朗的日子我们中的两个人做了什么”
,用户应该会看到“对不起,找到了坏单词”消息。如果
$string=“fine day”
,用户应该会看到string不包含任何不好的单词消息
正如我所知,您不能从数组中进行preg\u匹配。有什么建议吗?这个怎么样:
$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 { ... }
foreach($badwords as $badword)
{
if (strpos($string, $badword) !== false)
echo "sorry bad word found";
else
echo "string contains no bad words";
}
如果要通过将字符串分解为单词来检查每个单词,可以使用以下方法:
$badwordsfound = count(array_filter(
explode(" ",$string),
function ($element) use ($badwords) {
if(in_array($element,$badwords))
return true;
}
})) > 0;
if($badwordsfound){
echo "Bad words found";
}else{
echo "String clean";
}
现在,我想到了一个更好的方法,替换数组中所有的坏单词,并检查字符串是否保持不变,怎么样
$badwords_replace = array_fill(0,count($badwords),"");
$string_clean = str_replace($badwords,$badwords_replace,$string);
if($string_clean == $string) {
echo "no bad words found";
}else{
echo "bad words found";
}
下面是我使用的坏词过滤器,它非常有效:
private static $bad_name = array("word1", "word2", "word3");
// This will check for exact words only. so "ass" will be found and flagged
// but not "classic"
$badFound = preg_match("/\b(" . implode(self::$bad_name,"|") . ")\b/i", $name_in);
然后,我有另一个变量与要匹配的select字符串:
// This will match "ass" as well as "classic" and flag it
private static $forbidden_name = array("word1", "word2", "word3");
$forbiddenFound = preg_match("/(" . implode(self::$forbidden_name,"|") . ")/i", $name_in);
然后,我在其上运行if
:
if ($badFound) {
return FALSE;
} elseif ($forbiddenFound) {
return FALSE;
} else {
return TRUE;
}
希望这有帮助。询问是否需要我澄清任何事情。为什么要在此处使用preg\u match()
那么这个呢:
$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 { ... }
foreach($badwords as $badword)
{
if (strpos($string, $badword) !== false)
echo "sorry bad word found";
else
echo "string contains no bad words";
}
如果出于某些原因需要preg_match()
,可以动态生成正则表达式模式。大概是这样的:
$pattern = '/(' . implode('|', $badwords) . ')/'; // $pattern = /(one|two|three)/
$result = preg_match($pattern, $string);
HTH$string始终是一个随机字符串,更具体地说,它是一个搜索查询。所以,若访问者输入的查询中包含不好的词语,他应该看不到结果,否则。。你明白了吗?:)是的,你可以preg_match
一个数组,你只需要先对它进行内爆。请检查我给出的答案。我建议使用诸如“\b”之类的分隔符,否则,当后跟逗号或其他分隔符时,不好的单词将变得疯狂。)嘿,看看我在接受之前添加的第二个解决方案:Dbe小心这个解决方案的“clbuttic错误”。是的,我意识到了这一点(clbuttic),然后写了第二个解决方案,我认为这是非常实用的解决方案。谢谢@jtmith。我使用了你的阿拉伯字符串代码,效果很好。第二天,我试图运行代码,没有任何修改,但给了我错误的结果。问题出在preg_match函数中。需要帮忙吗?