检查值是否在数组中
这可能很简单,但我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从技术上讲,有一些选择,其他一些答案也指出了这些。但实际上,我想说不要这样做。
众所周知,脏话过滤器对于具有不寻常或外国名称/地址等的用户来说是令人沮丧和限制的。
请参阅: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?
这只会匹配一个坏词本身...即它会匹配
Is it bad?
中的bad
,但不会匹配baddy
中的bad
。如果您想匹配的话,可以删除单词边界,或者尝试strstr()
。但是,如果您决定检查简单字符串匹配,则有效名称可能包含被视为坏的子字符串。
如果我想用我的家乡来命名我的用户名,该怎么办?
This will match a bad word on its own only... i.e. it will match
bad
inIs it bad?
but not inbaddy
. You can remove the word boundaries if you want to match that, or perhaps trystrstr()
.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?
此代码将匹配通过搜索不区分大小写的匹配项找到的所有不良单词。
This code will match all bad words found by searching a case-insensative match.