Link to home
Start Free TrialLog in
Avatar of playstat
playstat

asked on

profanity filter from text file

Hi there

I have a list of words in a text file line by line and I need to use a text file that contains line by line profanity words. Then use that to find in another file text file and delete that line if it contains any of the words from the other file.

For example say the profanity word list is

air
bee
call

the text file with words is

air tree with leaves
bee fore you go there
I like trees
Not before dinner
every time after call
be have

then all that is left is

I like tree
not before dinner
be have

Can you make sure that the removed lines are collpased so they are after each other so there are no gaps in the output text file

thanks
 
ASKER CERTIFIED SOLUTION
Avatar of newaira
newaira

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Bear in mind that this method will also remove lines that contain bad words inside other words.  For example,  with your sample word list, you'll lose lines containing "Blair", "recall", "heebee-jeebies".  If that's a concern, you'll want to use regular expressions to check for word boundaries around the bad word.
Avatar of newaira
newaira

Thanks for clarifying snoyes_jw.
Avatar of playstat

ASKER

could you add them please it would be most helpful

thanks
replace the line
if ( false != stristr($lines[$i], $badword[$j]) ) {
with these two:
$pattern = "/(?i)\b" . $badword[$j] . "\b/";
if (preg_match($pattern, $lines[i])) {

I think...
This is the form for adding the files

<html>

<head>
<title>Keyword profanity List</title>
</head>
<form name='settings' method='post' enctype="multipart/form-data" action='profanity_filter.php'>
<body bgcolor="white" text="black" link="blue" vlink="purple" alink="red">
            <p>Keyword profanity List</p>

                <p><input type="file" name="profanity" size="20"></p>

            <p>Keyword file</p>

                <p><input type="file" name="keywordlist"></p>

<p>Outputfilename</p>

    <p><input type="text" name="fileoutput"></p>

    <p><input type="submit" name="submit"></p>
</form>
</body>

</html>

<?php
$profanity = $_POST['profanity'];
    $keywordlist = $_POST['keywordlist'];
    $fileoutput = $_POST['fileoutput'];

//read lines of the bad word file into array
$badword = file($profanity);

//remove newline character from each array value
for ($i=0; $i<count($badword); $i++)
  $badword[$i] = rtrim($badword[$i]);

//read lines of the file to check
$lines = file($keywordlist);

$count = count($lines);

for ($i=0; $i<$count; $i++) {
  for ($j=0; $j<count($badword);$j++) {
    if ( false != stristr($lines[$i], $badword[$j]) ) {
      unset($lines[$i]);
    }
  }
}

$lines = array_values($lines);
$lines = implode("", $lines);

$handle = fopen("$fileoutput.txt", "w");
fwrite($handle, $lines);
fclose($handle);

?>

All i get is this

Warning: stristr(): Empty delimiter. in C:\Program Files\nusphere\phped\Projects\profanity_filter.php on line 21

Warning: array_values(): The argument should be an array in C:\Program Files\nusphere\phped\Projects\profanity_filter.php on line 27

Warning: implode(): Bad arguments. in C:\Program Files\nusphere\phped\Projects\profanity_filter.php on line 28

Any ideas

its ok just finished it

thanks