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. Quote Link to comment Share on other sites More sharing options...
effigy Posted September 19, 2007 Share Posted September 19, 2007 See this topic. Quote Link to comment 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. Quote Link to comment 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/"; Quote Link to comment 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/'; Quote Link to comment 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! Quote Link to comment 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.