开发者

Blocking Cuss/Vulgar/Obscenity Terms in PHP

开发者 https://www.devze.com 2023-02-09 05:07 出处:网络
I know you might laugh, but actually this is a common need in most apps. Many apps that take in customer/visitor input may need to filter cuss words or vulgar terms.

I know you might laugh, but actually this is a common need in most apps. Many apps that take in customer/visitor input may need to filter cuss words or vulgar terms.

Sometimes PHP changes and new stuff gets added in. For instance, just the other day I learned about MultiCurl API in PHP5. So, anyway, is there a new native function in PHP that lets me filter most common English-based cuss words in a string, as well as flip a boolean to开发者_如何学Python say, "string had English-based cuss words in it"? It doesn't need to be perfect, obviously, but cut out a good bit of garbage and let me replace it with ### for instance.

If that's not part of PHP yet, then does anyone have a function that I can use which cloaks the cuss word list? For instance, I want it such that I can drop the class in a project and not have to worry about another programmer getting offended. In other words, a decently encoded cuss word list -- not one actually spelled out.

Now, obviously it needs to be flexible and let words like "rebuttal" get through.

tl;dr: Does PHP5 now have a native function that can filter obscene words? And if not, does anyone have a class that encodes a cuss word list so that it doesn't offend other programmers?


I doubt this is something that would be a high priority for the core PHP team since that treads dangerously close to censorship. Censorship in that they would have a 'master' list of 'inappropriate' language which should be filtered.

You can do this fairly simply. Make up an array of all the words you want filtered out and when a page is displayed that contains user input run a preg_filter() on the words.

$bad_words = array('bleeping', 'blooping'); $submitted_text = 'bleh blah....';

echo preg_filter($bad_words, $replace, $submitted_text);

Note: you will have to deal with the edge cases where a bad word might be inside of a good word (i.e.- 'shitzu[sic] dog')

EDIT For the bad-words-inside-good-words issue, you can add to the regular expression to require space at the beginning and end of the bad word. If you have lots of submissions though, it's going to be a constant battle to keep up with the trolls.


<?php
$badwords = "fuc";
$replacebad = "****"; 
$string = $_POST['something']; 
$filtered = str_ireplace($badwords, $replacebad, "$string");
echo $filtered;
?>

something like this ? Edit: sorry I didn't noticed the php5 part ..

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号