2014-01-12 66 views
-7

我一直想知道如何将在网站上注册的用户的用户名更改为完全不同的东西,如果它有不好的单词。我有与此类似的是这样的东西:PHP注册用户名更改

$BadWords = array("badword1", "badword2", "badword3", "badword4", "badword5", "badword6", "badword7", "badword8", "badword9", "badword10", "badword11", "badword12", "badword13", "badword14", "badword15", "badword16", "badword17", "badword18", "badword19", "badword20", "badword21", "badword22", "badword23", "badword24", "badword25", "badword26", "badword27", "badword28", "badword29","badphrase" ,"badword30","badword31","badword32"); 
$Var = "[ContentRemoved]"; 
$title = str_replace($BadWords,$Var, $Username); 
$Username = str_replace($BadWords,$Var, $Username); 

这样做的问题是,如果有人输入用户名,如“gobadword23”将其改为“去[ContentRemoved]”。

我希望它做的是用“[ContentRemoved]”替换WHOLE用户名。而不仅仅是坏话。

+0

不好主意,总是可以绕过。相当令人反感,你认为这些词的一些'坏' – 2014-01-12 22:27:37

+0

你看过吗? http://stackoverflow.com/questions/4366730/how-to-check-if-a-string-contains-specific-words – x20mar

+0

“分析”,“犹太人”,“公鸡”和“玻璃”这些词都是禁止的太? – halfer

回答

0

这应该做的伎俩:

foreach ($BadWords as $word) { 
    if (stripos($Username, $word) !== false) { 
     $Username = '[ContentRemoved]'; 
    } 

    if (stripos($title, $word) !== false) { 
     $title = '[ContentRemoved]'; 
    } 
} 

我以前stripos()代替strpos()所以匹配大小写不敏感(所以你不能简单地通过以大写字母输入所有内容来绕过过滤器)。

+0

然后,我会如何制作它,以便在“已删除”后将一个与该人的ID相同的号码,以便它不会将您重定向到相同的配置文件? – user3188415

+0

您只需使用基本的PHP字符串连接,如下所示:'$ username ='[ContentRemoved'。 $ UserID。 ']';'。当然,你必须首先定义'$ UserID'。请参阅http://www.php.net/manual/en/language.operators.string.php – hanzi

+0

它必须将1添加到数据库中最大的ID,对吗? – user3188415

1

嗯,首先,感谢那个美妙的作品。照亮我的一天。

Using an array as needles in strpos

阅读线程,然后它会是那么容易,因为

if(strpos($haystack, $needle)) { 
$username = '[ContentRemoved]'; 
}