3raser Posted February 21, 2012 Share Posted February 21, 2012 I'm pretty sure the problem is starring me in the face, but I can't seem to locate it. function filter($string) { //swear words pulled from bannedwordlist.com $f = fopen('../badwords.txt', 'r'); $bad_words = fread($f, filesize('../badwords.txt')); $bad_words = explode('\n', $bad_words); $input = strtolower($string); foreach($bad_words as $value) { $string = str_replace($value, '****', $input); } return $string; } UPDATED CODE function filter($string) { //swear words pulled from bannedwordlist.com $f = fopen('../badwords.txt', 'r'); $bad_words = fread($f, filesize('../badwords.txt')); $bad_words = explode('\n', $bad_words); $input = strtolower($string); $string = str_replace($bad_words, '****', $input); return $string; } I've already echoed out $value in the foreeach loop, and it does correctly retrieve the bad words and put them into an array. My only problem is, the returned string is still in strtolower() form, and the words aren't censored. :/ Quote Link to comment https://forums.phpfreaks.com/topic/257495-not-filtering-words/ Share on other sites More sharing options...
xyph Posted February 22, 2012 Share Posted February 22, 2012 Since strtolower() isn't always reversible, you'd be better off using str_ireplace Also, Windows (\r\n) handles line-breaks differently than other operating systems (\n only), so if you explode by \n and your lines are terminated with \r\n, you will end up with "curseword\r" array values. This example works properly <?php $search = array('one','two','three'); $body = 'This one, those two, make three'; $body = str_ireplace($search, '***', $body); echo $body; ?> so I'm going to assume it's a problem with your array values. Quote Link to comment https://forums.phpfreaks.com/topic/257495-not-filtering-words/#findComment-1319778 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.