Solved

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

Posted on 2008-10-16
5
335 Views
Last Modified: 2012-05-05
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
Comment
Question by:baumli1
  • 3
5 Comments
 

Author Comment

by:baumli1
ID: 22732994
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
 

Author Comment

by:baumli1
ID: 22733206
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
 

Author Comment

by:baumli1
ID: 22733362
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
 
LVL 8

Accepted Solution

by:
aldanch earned 50 total points
ID: 22736740

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ajax call to redirect to a different page with data from controller 7 50
mysqli 3 23
Access Object's Property 9 22
hasing a url 16 8
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now