bdichiara Posted September 19, 2007 Share Posted September 19, 2007 I've got a function that is being used to filter messages before they're inserted into a database that gets rid of any foul language and replaces it with either **** or an alternate word/phrase. The problem is it replaces parts of other words. For example, the word Assault gets replaced as: ***ault. Can someone please help me modify this so it only replaces whole words? I was thinking maybe putting spaces around the word to check it, but this will cause it to fail on words at the beginning of the text or with punctuation after or around the word (like "bleep" or bleep.) here's the function: function filterContent($content){ $filename = "../../foul_language.txt"; if(file_exists($filename)){ $fh = @fopen($filename, 'r'); $obscenities = @fread($fh, filesize($filename)); $obscenities = explode("\n",$obscenities); $content = trim($content); foreach ($obscenities as $curse_word) { $curse_word = trim($curse_word); $curse_word = explode(";",$curse_word); $word = $curse_word[0]; $replace = $curse_word[1]; if (stristr($content,$word)){ if(empty($replace)){ $length = strlen($word); $replace = str_repeat("*",$length); } // Replace the dirty string with the filtered string $content = eregi_replace($word, $replace, $content); } $replace = ""; } } return $content; } And my file is setup like this: word; word;alternate if it's blank after the ";", it uses *****. Thanks. Link to comment https://forums.phpfreaks.com/topic/69909-solved-offensive-wordphrase-filter/ Share on other sites More sharing options...
effigy Posted September 19, 2007 Share Posted September 19, 2007 See this topic. Link to comment https://forums.phpfreaks.com/topic/69909-solved-offensive-wordphrase-filter/#findComment-351116 Share on other sites More sharing options...
bdichiara Posted September 19, 2007 Author Share Posted September 19, 2007 i'm confused. I tried using this, but when it matches, it just returns an entire blank line: $expr = '\b$word\b'; $content = preg_replace($expr,$replace,$content); also tried this: $expr = '\b'.$word. '\b'; And I think it's blank because our php display errors are turned off... and unforturtunately, it stopped logging them in the error log a few months ago and I can't figure out why. but that's another story. Link to comment https://forums.phpfreaks.com/topic/69909-solved-offensive-wordphrase-filter/#findComment-351139 Share on other sites More sharing options...
effigy Posted September 19, 2007 Share Posted September 19, 2007 All patterns require delimiters, and variables are not interpolated in single quotes. Try this: $expr = "/\b$word\b/"; Link to comment https://forums.phpfreaks.com/topic/69909-solved-offensive-wordphrase-filter/#findComment-351142 Share on other sites More sharing options...
bdichiara Posted September 19, 2007 Author Share Posted September 19, 2007 That didn't work, but this did: $expr = '/\b'.$word.'\b/'; Link to comment https://forums.phpfreaks.com/topic/69909-solved-offensive-wordphrase-filter/#findComment-351150 Share on other sites More sharing options...
bdichiara Posted September 19, 2007 Author Share Posted September 19, 2007 Ah, ok, i just read what you said about single quotes. I never knew that. Honestly, i rarely put my variable inside the quotes. It's hard for me to see them. Thanks for the help! Link to comment https://forums.phpfreaks.com/topic/69909-solved-offensive-wordphrase-filter/#findComment-351154 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.