检查值是否在数组中

发布于 2024-10-21 16:58:01 字数 243 浏览 1 评论 0原文

这可能很简单,但我的 php 很生疏。我需要实现一个坏词过滤器...我目前有这个:

if(!in_array($_POST['Name'],$profanities)) { echo $row['Name']; }

...以及一个包含坏词的脏话数组。问题是我希望能够检查用户输入的名称的任何部分是否包含这些坏词之一....我目前只检查该名称是否是确切的坏词....如何我这样做吗?

谢谢

This is probably v.easy but my php is very rusty. I need to implement a bad word filter...I currently have this:

if(!in_array($_POST['Name'],$profanities)) { echo $row['Name']; }

...and an profanities array with bad words in it. Problem is I want to be able to check if any portion of the name the user has entered contains one of those bad words....what I've currently got only checks if the name is the exact bad word....how do i do this?

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

感受沵的脚步 2024-10-28 16:58:01

从技术上讲,有一些选择,其他一些答案也指出了这些。但实际上,我想说不要这样做。

众所周知,脏话过滤器对于具有不寻常或外国名称/地址等的用户来说是令人沮丧和限制的。

请参阅:http://en.wikipedia.org/wiki/Swear_filter#Unintend_consequences

此外,如果您的用户想要找到绕过过滤器的方法,他们会的。他们将使用发音相似的字符或特殊字符作为替换和添加。

编辑:有一个很好的 stackoverflow 问题已经解决了这个问题。请参阅如何实现良好的脏话过滤器?

编辑:虽然上面的 stackoverflow 问题引用了 Jeff 的帖子,但重复和强化也没什么坏处。 淫秽过滤器:坏主意,还是令人难以置信的相互影响的坏主意?

Technically there are a few options, and some other answers point these out. Realistically, however, I would say do not do this.

Profanity filters are notoriously frustrating and limiting for users with unusual or foreign names/address/etc.

Please see: http://en.wikipedia.org/wiki/Swear_filter#Unintended_consequences

Additionally, if your users want to find a way around the filter, they will. They'll use similar sounding characters or special characters as both substitutions and additions.

EDIT: There is an excellent stackoverflow question already addressing this issue. please see How do you implement a good profanity filter?

EDIT: although the above stackoverflow question refers to Jeff's post about this, it can't hurt to repeat and reinforce. Obscenity Filters: Bad Idea, or Incredibly Intercoursing Bad Idea?

心凉 2024-10-28 16:58:01
$name = $_POST['Name'];

foreach($profanities as $profanity) {

   if (preg_match('/\w' . preg_quote($profanity, '/') . '\w/i', $name) {
      // Bad word on its own
   }

}

这只会匹配一个坏词本身...即它会匹配Is it bad?中的bad,但不会匹配baddy中的bad。如果您想匹配的话,可以删除单词边界,或者尝试 strstr()

但是,如果您决定检查简单字符串匹配,则有效名称可能包含被视为的子字符串。

如果我想用我的家乡来命名我的用户名,该怎么办?

$name = $_POST['Name'];

foreach($profanities as $profanity) {

   if (preg_match('/\w' . preg_quote($profanity, '/') . '\w/i', $name) {
      // Bad word on its own
   }

}

This will match a bad word on its own only... i.e. it will match bad in Is it bad? but not in baddy. You can remove the word boundaries if you want to match that, or perhaps try strstr().

However, a valid name may contain a substring that is considered bad if you decide to check for simple string matching.

What if I wanted to name my username after my hometown?

桜花祭 2024-10-28 16:58:01

此代码将匹配通过搜索不区分大小写的匹配项找到的所有不良单词。

foreach($profanities as $profanity)
{
    if(preg_match('/' . $profanity . '/i', $row['Name']))
    {
        echo 'Bad word found!';
    }
}

This code will match all bad words found by searching a case-insensative match.

foreach($profanities as $profanity)
{
    if(preg_match('/' . $profanity . '/i', $row['Name']))
    {
        echo 'Bad word found!';
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文