POC0bob Posted January 25, 2013 Share Posted January 25, 2013 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! Quote Link to comment https://forums.phpfreaks.com/topic/273649-word-filter/ Share on other sites More sharing options...
requinix Posted January 25, 2013 Share Posted January 25, 2013 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); Quote Link to comment https://forums.phpfreaks.com/topic/273649-word-filter/#findComment-1408283 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.