Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 342
  • Last Modified:

PHP string search and substitution, works with array, doesn't work with file

I am trying to get a language filter work.  It searches the string to see if any words exist from the array, then it converts them to *.  I can get it to work with an array, however I can't get it to work when I try to use a file.
It is opening the file, it reads in the array, the line that doesn't work is this:
        if (stristr(trim($string),$curse_word)) {
//Foul Language Function Start
function language_filter($string) {
//  Array Works
//  $obscenities = array("curse","word"," foul ","language");
 
//  Array in File Doesn't Work
	$filename = "foul_language.txt";
	$fp = fopen($filename,'r'); 
	if (!$fp) {echo 'ERROR: Unable to open file.'; exit;} 
       $obscenities = @file($filename);
//  Substitute words
    foreach ($obscenities as $curse_word) {
        if (stristr(trim($string),$curse_word)) {
            $length = strlen($curse_word);
            for ($i = 1; $i <= $length; $i++) {
                $stars .= "*";
            }
            $string = eregi_replace($curse_word,$stars,trim($string));
            $stars = "";
        }
    }
    return $string;
}
//Foul Language Function Stop

Open in new window

0
baumli1
Asked:
baumli1
  • 3
1 Solution
 
baumli1Author Commented:
The foul_language.txt contains the following lines, but it reads them in as an array properly.
"curse"
curse
 curse 

Open in new window

0
 
baumli1Author Commented:
It has something to do with the array in the text file.  I thought I could just put one entry per line, but its not reading more then 1 line.  If I just put one word in the file, with no return, it works, but as soon as I put in another line, it stops working.
0
 
baumli1Author Commented:
I did get it to work, but if someone else has any ideas as to how I should clean it up or impove it, I'll leave the question open.
//Foul Language Function Start
function language_filter($string) {
    $filename = "foul_language.txt";
    $fp = @fopen($filename, 'r'); 
    if ($fp) {  $obscenities = explode("\n", fread($fp, filesize($filename))); }
//  Substitute words
    foreach ($obscenities as $curse_word) {
        if (@stristr(trim($string),$curse_word)) {
            $length = strlen($curse_word);
            for ($i = 1; $i <= $length; $i++) {
                $stars .= "*";
            }
            $string = eregi_replace($curse_word,$stars,trim($string));
            $stars = "";
        }
    }
    return $string;
}
//Foul Language Function Stop

Open in new window

0
 
aldanchCommented:

function language_filter($string) {
	$badwords = array();
	$badwords = explode("\n", file_get_contents("/path/to/badwords.txt"));
	foreach($badwords as $word) {
		$string = str_replace($word, str_pad("*", strlen($word), "*", STR_PAD_LEFT), $string);
	}
	return $string;
}

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now