A word filter not working because of the function 'file'

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

A word filter not working because of the function 'file'

Post by impulse() »

I have wrote a script that takes an input and splits that input on each space so it leaves the input as an array.
I then have another array which contains words from a file. I'm using

Code: Select all

file
to read the file and the file has a single word per line but the array doesn't only contain the word from the file, it also contains a newline character.

My code is as follows:

Code: Select all

$file = file("banned.txt");

$input = "the only word not allowed in this string is banned";
$input = explode(" ", $input);

for ($i = 0; $i < count($file); $i++) {
  for ($j = 0; $j < count($input); $j++) {
    if ($file[$i] == $input[$j]) {
      echo $file[$i], " matches ", $input[$j], "\n";
    }
    else {
      echo $file[$i], " doesn't match ", $input[$j], "\n";
    }
  }
}
And banned.txt looks like this:

banned
words
The output from this script is:
banned
doesn't match the
banned
doesn't match only
banned
doesn't match word
banned
doesn't match not
banned
doesn't match allowed
banned
doesn't match in
banned
doesn't match this
banned
doesn't match string
banned
doesn't match is
banned
doesn't match banned
words doesn't match the
words doesn't match only
words doesn't match word
words doesn't match not
words doesn't match allowed
words doesn't match in
words doesn't match this
words doesn't match string
words doesn't match is
words doesn't match banned
As you can see, 'banned' also contains a newline character which is causing it not to match with the word from the string.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

file() does not remove the end of line characters. You need to do that yourself with say trim().

For proof, use var_dump(). Pay special attention to the lengths it says the strings are. ;)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

array_map('trim', $file);
might be of interest :wink:
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

I tried both ways but array_map seemed to work better for me. Now I've overcome that problem I've encountered another.

Here's the code:

Code: Select all

$bannedWords = file("banned.txt");
    $bannedWords = array_map('trim', $file);

    $splitComments = explode(" ", $comments);

    for ($i = 0; $i <= count($splitComments); $i++) {
      for ($j = 0; $j <= count($bannedWords); $j++) {
        if ($splitComments[$i] == $bannedWords[$j]);
          $dontPost = 1;
          echo $splitComments[$i], " matches ", $bannedWords[$j], "<br>";
        }
      }
    }
But this causes the code to run

Code: Select all

if ($splitComments[$i] == $bannedWords[$j]);
even when the words don't match. I got it to echo what equalled and it echoed 2 blank entries. Although there are no blanks inside of banned.txt nor did I enter any blank words into $comments. $comments grabs is contents from a form text box via POST.

I also tried changing

Code: Select all

if ($splitComments[$i] == $bannedWords[$j]);
to

Code: Select all

if ($splitComments[$i] == $bannedWords[$j] && $splitComments[$i] != "" && $bannedWords[$j] != "");
and this causes it to never run the banned words code.

Any ideas?
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post by iknownothing »

maybe white space?
try disregarding " " and see how you go.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

impulse() wrote:I tried both ways but array_map seemed to work better for me. Now I've overcome that problem I've encountered another.

Here's the code:

Code: Select all

$bannedWords = file("banned.txt");
    $bannedWords = array_map('trim', $file);

    $splitComments = explode(" ", $comments);

    for ($i = 0; $i <= count($splitComments); $i++) {
      for ($j = 0; $j <= count($bannedWords); $j++) {
        if ($splitComments[$i] == $bannedWords[$j]);
          $dontPost = 1;
          echo $splitComments[$i], " matches ", $bannedWords[$j], "<br>";
        }
      }
    }
But this causes the code to run

Code: Select all

if ($splitComments[$i] == $bannedWords[$j]);
even when the words don't match. I got it to echo what equalled and it echoed 2 blank entries. Although there are no blanks inside of banned.txt nor did I enter any blank words into $comments. $comments grabs is contents from a form text box via POST.

I also tried changing

Code: Select all

if ($splitComments[$i] == $bannedWords[$j]);
to

Code: Select all

if ($splitComments[$i] == $bannedWords[$j] && $splitComments[$i] != "" && $bannedWords[$j] != "");
and this causes it to never run the banned words code.

Any ideas?
:?

Did you mean to have that semi-colon after the if statement?

Code: Select all

if ($splitComments[$i] == $bannedWords[$j]);
What's that supposed to do?
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

if ($splitComments[$i] == $bannedWords[$j]);
This checks whether an entered word from splitComments is in the banned list.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

impulse() wrote:
if ($splitComments[$i] == $bannedWords[$j]);
This checks whether an entered word from splitComments is in the banned list.
uhh.. your not getting a parse error? you should basically replace the colon with a curly bracket to match up, as it is your saying

if ( ... ) do nothing;
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

I did replace it earlier, I must've copied and pasted the code here before I changed it.
The semi-colon is now gone. I have hard coded the words from banned.txt into an array and the code works fine. It must be a matter of newlines and white spaces in the banned.txt file. I'll get it all sorted, I should be OK from here, hopefully. All the information I need is in this thread.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

btw-- why do some lines have multiple words? Why not simply make each word have it's own line.

Then you can simply do this nasty one liner.. you should likely split it up a bit though..

Code: Select all

if (count(array_intersect(explode(' ', $comments), file('banned.txt')) > 0)
{
   echo 'your a potty mouth';
}
Post Reply