Page 1 of 1
A word filter not working because of the function 'file'
Posted: Tue Dec 19, 2006 4:50 pm
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
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.
Posted: Tue Dec 19, 2006 4:53 pm
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.

Posted: Tue Dec 19, 2006 5:00 pm
by John Cartwright
might be of interest

Posted: Wed Dec 20, 2006 6:19 am
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?
Posted: Wed Dec 20, 2006 8:07 am
by iknownothing
maybe white space?
try disregarding " " and see how you go.
Posted: Wed Dec 20, 2006 9:47 am
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?
Posted: Wed Dec 20, 2006 10:37 am
by impulse()
if ($splitComments[$i] == $bannedWords[$j]);
This checks whether an entered word from splitComments is in the banned list.
Posted: Wed Dec 20, 2006 10:52 am
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;
Posted: Wed Dec 20, 2006 11:06 am
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.
Posted: Wed Dec 20, 2006 11:10 am
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';
}