View Full Version : Can't load text file into array

Retired Bill
09-27-2006, 04:53 PM
I have adapted this function to filter profanity from a guestbook. Somehow the text file does not fill the array. Each entry in the file ends with a newline. If I define the array in the function, the function works fine, replacing the offensive words with *s. Can someone tell me what I might have done wrong with either the file or the function?

function language_filter($comments)
$obscenities = file("foul_language.txt");
foreach ($obscenities as $curse_word)
{if (stristr(trim($comments),$curse_word))
for ($i = 1; $i <= $length; $i++)
$stars .= "*";
$comments = eregi_replace($curse_word, $stars, trim($comments));
$stars = "";
return $comments;

09-27-2006, 05:10 PM
I believe the "\n" is going to be part of each element in your $obscenities array. Try using trim() on $curse_word.

Retired Bill
09-28-2006, 08:02 PM
That did it. Thanks!