Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

profanity filter from text file

Posted on 2004-08-06
7
Medium Priority
?
1,311 Views
Last Modified: 2008-01-09
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
 
0
Comment
Question by:playstat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 3

Accepted Solution

by:
newaira earned 1500 total points
ID: 11736089
try this:

<?php

//read lines of the bad word file into array
$badword = file('badwords.txt');

//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('listofwords.txt');

$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("newlistofwords.txt", "w");
fwrite($handle, $lines);
fclose($handle);

?>
0
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 11736518
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.
0
 
LVL 3

Expert Comment

by:newaira
ID: 11736608
Thanks for clarifying snoyes_jw.
0
Technology Partners: 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!

 

Author Comment

by:playstat
ID: 11737615
could you add them please it would be most helpful

thanks
0
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 11737726
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...
0
 

Author Comment

by:playstat
ID: 11738044
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

0
 

Author Comment

by:playstat
ID: 11738409
its ok just finished it

thanks
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

618 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