可能重复:
测试字符串中某些单词的有效方法
我想检查字符串是否包含以下单词中的任何一个:ban、bad、user、pass、stack、name、html。
如果它包含任何单词,我需要回显坏单词的数量
str = 'Hello my name is user';
我认为这样的东西会起作用:
$badWords = array("ban","bad","user","pass","stack","name","html");
$string = "Hello my name is user.";
$matches = array();
$matchFound = preg_match_all(
"/'b(" . implode($badWords,"|") . ")'b/i",
$string,
$matches
);
if ($matchFound) {
$words = array_unique($matches[0]);
foreach($words as $word) {
echo "<li>" . $word . "</li>";
}
echo "</ul>";
}
这创建了一个被禁止的单词数组,并使用正则表达式来查找这些单词的实例:
Regex中的
'b
表示单词边界(即单词的开头或结尾,由字符串的开头/结尾或非单词字符确定(。这样做是为了防止"clbuttic"错误,即当你只想匹配单词"ban"时,你不想禁止单词"banner"。implode
函数创建一个字符串,其中包含所有被禁止的单词,并用竖线字符分隔,竖线字符是Regex中的or
运算符。Regex的
implode
部分用括号包围,因此preg_match_all
将捕获被禁止的单词作为匹配。Regex末尾的
i
修饰符表示匹配应该区分大小写,即它将匹配每个单词,而不考虑大写-"Ban"、"Ban"answers"Ban"都将与$badWords
数组中的单词"Ban"匹配。
接下来,代码将检查是否找到任何匹配项。如果有,它使用array_unique
来确保每个单词只报告一个实例,然后输出无序列表中的匹配列表。
这就是你要找的吗?
这就是您想要的。
function teststringforbadwords($string,$banned_words) {
foreach($banned_words as $banned_word) {
if(stristr($string,$banned_word)){
return false;
}
}
return true;
}
$string = "test string";
$banned_words = array('ban','bad','user','pass','stack','name','html');
if (!teststringforbadwords($string,$banned_words)) {
echo 'string is clean';
}else{
echo 'string contains banned words';
}
-
模式中的''b表示单词边界,因此只有不同的单词"web"是匹配的,而不是像"webbing"或"cobweb"那样的单词分部
if(preg_match("/''bweb''b/i","PHP是首选的web脚本语言。"({echo"找到匹配项。";}其他{echo"未找到匹配项。";}
if (preg_match("/'bweb'b/i", "PHP is the website scripting language of choice.")) { echo "A match was found."; } else { echo "A match was not found."; }
这是你最好的选择。正如开头所说,你可以控制你的正则表达式。
这是直接从php.net
function check_words($text) {
$text=$text;
$bad_words = file('bad_words.txt');
$bad = explode(" | ",$bad_words[0]);
$b = '/'W' . implode(''W|'W', $bad) . ''W/i';
if(preg_match($b, $text)){
echo $text ." - Contain Bad words!"; other function here
} else {
echo $text ." - Not containing bad words :D";
// other function here
}
}
示例:check_words('He is good');
尽管最终的/
之后的任何内容似乎都没有被检查,例如http://www.mysite.com/thisbit
、thisbit
似乎没有被检查坏词,但这仍然很有效。
如果它是这样键入的:http://www.mysite.com/thisbit/
,后面是/
,它会再次工作。
不确定这是否可以修复。
function check_words($text) {
$text=$text;
$bad_words = file('bad_words.txt');
$bad = explode(" | ",$bad_words[0]);
$b = '/'W' . implode(''W|'W', $bad) . ''W/i';
if(preg_match($b, $text)){
echo $text ." - Contain Bad words!";
# - other function here
}
else{
echo $text ." - Not containing bad words :D";
# - other function here
}
}
# - Example
check_words('He is good');
希望这能有所帮助。。您可以将所有的坏单词放在badwords.txt文件中。
将txt中的坏单词排列为:
bad_words1 | bad_words2 | bad_words3 | bad_words4 ...
注意:你也可以放一些类似的东西:
bad words 1 | bad words 2 | bad words 3
只要是"|"格式。