检查字符串中是否有坏词? [英] Check a string for bad words?

查看:62
本文介绍了检查字符串中是否有坏词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
测试某些单词的字符串的有效方法

Possible Duplicate:
Efficient way to test string for certain words

我想检查一个字符串是否包含以下任何单词:禁止,错误,用户,通过,堆栈,名称,html .

I want to check if a string contains any of these words: ban, bad, user, pass, stack, name, html.

如果其中包含任何单词,我需要回显坏单词的数量

If it contains any of the words I need to echo the number of bad words

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>";
}

这会创建一系列禁词,并使用正则表达式查找这些单词的实例:

This creates an array of banned words, and uses a regular expression to find instances of these words:

    正则表达式中的
  • \b表示单词边界(即单词的开头或结尾,由字符串的开头/结尾或非单词字符确定).这样做是为了防止"clbuttic"错误-即您不想要当您只想匹配禁令"一词时,禁止禁令"一词.

  • \b in the Regex indicates a word boundary (i.e. the beginning or end of a word, determined by either the beginning/end of the string or a non-word character). This is done to prevent "clbuttic" mistakes - i.e. you don't want to ban the word "banner" when you only want to match the word "ban".

implode 函数创建一个字符串包含所有禁止的单词,并用竖线字符分隔,即正则表达式中的or运算符.

正则表达式的implode部分用括号括起来,以便 preg_match_all 将捕获被禁止的单词作为匹配项.

The implode portion of the Regex is surrounded with parentheses so that preg_match_all will capture the banned word as the match.

正则表达式末尾的i修饰符表示匹配应区分大小写-即,它将匹配每个单词,而不考虑大小写-"Ban,"ban"和"BAN"将全部与$badWords数组中的禁令"匹配.

The i modifier at the end of the Regex indicates that the match should be case-sensitive - i.e. it will match each word regardless of capitalization - "Ban, "ban", and "BAN" will all match against the word "ban" in the $badWords array.

接下来,代码检查是否找到任何匹配项.如果有的话,它使用 array_unique 来确保只有一个报告每个单词的实例,然后以无序列表的形式输出匹配项列表.

Next, the code checks if any matches were found. If there are, it uses array_unique to ensure only one instance of each word is reported, and then it outputs the list of matches in an unordered list.

这是您要找的吗?

这篇关于检查字符串中是否有坏词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆