Jump to content

Word filter


POC0bob

Recommended Posts

So what I need is a function that filters out words, in a text file (new word on each line) and replaces all but the first letter with starts, like if i where to say asswipe, i need to to replace it with a****** the thing is, I also need it to not be case sensitive, and look for repetitive letters, say someone said assswipe.. that second part isn't required - it would just be nice. All i have so far is

 

function LangFilter($string) {
$badwords=fopen("./langfilter.txt","r");
for ($i=0;$i < count($badwords);$i++){ 
$string = str_replace($badwords[$i],str_repeat("*",strlen($badwords[$i])),$string); 
} 
return $string; 
} 

 

But it doesn't work. The reason im not saying its a cuss word filter, is there are some other things I would need it to filter, but ill do those my self. I looked on a few other topics, but none of those had what I needed... Any help? Thanks!

Link to comment
https://forums.phpfreaks.com/topic/273649-word-filter/
Share on other sites

Given the word "asswipe" you can do a replace with regular expressions.

$input = "Bob is such an aasssswwiipee";

$word = "asswipe";

$matchword = implode("+", array_map("preg_quote", str_split($word))) . "+"; // a+s+s+w+i+p+e+
$output = preg_replace_callback('/\b' . $matchword . '\b/i', function($matches) {
return $matches[0][0] . (strlen($matches[0]) > 1 ? str_repeat("*", strlen($matches[0]) - 1) : "");
}, $input);

Link to comment
https://forums.phpfreaks.com/topic/273649-word-filter/#findComment-1408283
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.