A piece of short script that I wrote days ago
function word_filter($string)
{
// Use file_get_contents() to get the source of bad words
$bad_word = file_get_contents("./bad_words.txt");
// Replace each new line as a seperator that preg_replace
// can accept
$bad_word = preg_replace('/[\r\n]+/', '|', $bad_word);
// Replace all bad words as [beep]
$string = preg_replace("/($bad_word)/i", '[beep]', $string);
return $string;
}
And the text file is the source of a list of bad words (one word per line)
I just found a problem, the first word in the first line cannot be filtered, it looks like there must be a seperator before the first word as well. So the only solution I figured out is to make the first line blank (as I need to add a seperator before the first word)
Can anyone please help me on this?