问题
I'm building a web app with php and the codeigniter framework, and one of the requirements for the client is to implement a 'swear word' filter on from validation. Does anyone know of any prebuilt solutions written in php? Or alternatively, an exhaustive list of curse words that I can format into a php syntaxed array and check for their presence in a user's input? Thanks!
回答1:
This should be able to get you started with the basic idea, but you'll have to fill the bad words in yourself. You can download a list of bad words here.
However, I must warn you that swear-word filters have some insurmountable issues. The English language is too flexible to prevent people from spelling swear words in a host of different ways. Even if you can block @$$h01e
, you'll never reliably block αṡṡhølε
, which can easily be produced by a character translator. There's no way you can even begin to filter out all the possibilities when each word and letter can have 20+ potential substitutes.
Plus, once people realize you have a swear-word filter, it may become a game to figure out how to thwart it, which can lead to more swearing than you started with! If people want to swear, they will find a way to do so. Fortunately, there are plenty of sites that have already dealt with this problem. What you can do is make them not want to swear by giving them the best experience with your website and content possible, and providing a simple flag feature so users can notify you of the unpreventable cases.
Stackoverflow has no curse word filter, and how often do you see swearing here? Don't send a computer to do a human's job. :D
回答2:
I absolutely hate that this exists, but there it is:
http://www.noswearing.com/about.php
回答3:
This is a profanity filter I made for a client if this helps anyone let me know :D
// function use cleanItUp($cmt:String,$wordlist:String,$character:string,$returnCount:boolean)
//$cmt
// Expects a string;
// ex:'this is a lovely day to walk the dog'
//$wordlist
// Expects a list of words to be replaced seporated by a verticle line '|'
// default world list provided;
// ex:'is|fun|good|dog'
//$character
// Expects a single character to replace each character of the replaced word
// default is an asterix '*'
//$returnCount
// Expects true if you would only like to return the amount of words that were replaced;
// default is set to false
// Usage Example
// using default word list
// cleanItUp('this is a lovely day to walk the dog');
// returns 'this is a lovely day to walk the dog'
// using custom wordlist
// cleanItUp('this is a lovely day to walk the dog','is|day|dog');
// returns 'this ** a lovely *** to walk the ***'
// using custom wordlist and character
// cleanItUp('this is a lovely day to walk the dog','is|day|dog','#');
// returns 'this ## a lovely ### to walk the ###';
// using custom wordlist and setting $returnCount to true
// cleanItUp('this is a lovely day to walk the dog','is|day|dog','#',true);
// returns 3;
function cleanItUp($cmt,$wordlist=null,$character="*",$returnCount=false)
{
if($wordlist==null)
{
$wordlist="nigga|nigger|niggers|sandnigger|sandniggers|sandniggas|sandnigga|honky|honkies|chink|chinks|gook|gooks|wetback|wetbacks|spick|spik|spicks|spiks|bitch|bitches|bitchy|bitching|cunt|cunts|twat|twats|fag|fags|faggot|faggots|faggit|faggits|ass|asses|asshole|assholes|shit|shits|shitty|shity|dick|dicks|pussy|pussies|pussys|fuck|fucks|fucker|fucka|fuckers|fuckas|fucking|fuckin|fucked|motherfucker|motherfuckers|motherfucking|motherfuckin|mothafucker|mothafucka|motherfucka";
}
$replace = 'preg_replace("/./","' .$character .'","\\1")';
$comment = preg_replace("/\b($wordlist)\b/ie", $replace,$cmt,-1,$count);
if($returnCount!=false)
{
return $count;
}
elseif($returnCount!=true)
{
return $comment;
}
}
来源:https://stackoverflow.com/questions/6549197/php-form-curse-word-input-filter-solution