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
evilmonkey
Forum Regular
Posts: 823 Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada
Post
by evilmonkey » Thu Sep 16, 2004 3:16 pm
Hello. I'm trying to make a system that will filter bad words from usernames. I have a list of these naughty words in a text file, and I want to use regex to check each one of those words against a user's desired username. Here's what I have so far:
Code: Select all
<?php
$file = fopen("badwords.txt", "r");
while(!feof($file)){
$badwords[] = fread($file, 10000);
if (preg_match("/[\w\.]$badwords[][\w\.])){
$error=1;
break;
}
}
?>
however, the problem is that my regex assumes that there is something before and after the word. How can I make those parameters optional? Also, can someone help me out with reading stuff from a file? I think that code is horribly wrong too...My file has a bunch of words on separate lines.
Thanks!
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Thu Sep 16, 2004 3:23 pm
why not use [php_man]file[/php_man] or [php_man]file_get_contents[/php_man] .. then smash the words together into a regex and then do 1 single preg run?
how's this file set up? new line for each word?
Code: Select all
<?php
$lines = file('badwords.txt');
$words = '';
foreach($lines as $line)
$words .= (empty($word) ? '' : '|') . preg_quote(trim($line), '#');
if(preg_match('#\b(' . $words . ')\b#i' , $name))
die(' bad name! ');
?>
evilmonkey
Forum Regular
Posts: 823 Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada
Post
by evilmonkey » Thu Sep 16, 2004 8:42 pm
Thanks feyd. Yes, it is a new line for each word. I tried your code like this:
Code: Select all
<?php
$lines = file('badwords.txt');
$name = "badword";
$words = '';
foreach($lines as $line){
$words .= (empty($word) ? '' : '|') . preg_quote(trim($line), '#');
}
if(preg_match('#\b(' . $words . ')\b#i' , $name)){
die(' bad name! ');
}
?>
But the script never died....I really don't vget the regex statement either.
Can you explain it to me plase? Thanks!
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Thu Sep 16, 2004 8:54 pm
well it's written to pick out full words.. so if you want them to match inside anything, then remove the \b's.. the whole regex rendered out, should look like: '#(some|words|here)#i' (if you remove the \b's)
Weirdan
Moderator
Posts: 5978 Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine
Post
by Weirdan » Thu Sep 16, 2004 8:55 pm
feyd mistyped empty(... part. that should be:
Code: Select all
//....
$words .= (empty($words) ? '' : '|') . preg_quote(trim($line), '#');
//....
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Thu Sep 16, 2004 8:56 pm
evilmonkey
Forum Regular
Posts: 823 Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada
Post
by evilmonkey » Thu Sep 16, 2004 9:07 pm
Hmm, thanks for your help, however, I still have a small problem. It now gives me "bad name" no matter what:
Code: Select all
<?php
$lines = file('badwords.txt');
$name = "jack";
$words = '';
foreach($lines as $line){
$words .= (empty($words) ? '' : '|') . preg_quote(trim($line), '#');
}
if(preg_match('#(' . $words . ')#i' , $name)){
die(' bad name! ');
}
?>
my file looks like this:
I don't know how happy people will be with me posting the actual words, so I just replaced them.
Thanks!
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Thu Sep 16, 2004 9:11 pm
maybe you have an empty line in the file... check a rendering of $words
evilmonkey
Forum Regular
Posts: 823 Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada
Post
by evilmonkey » Thu Sep 16, 2004 9:13 pm
Hmm, echo $words; comes up blank...
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Thu Sep 16, 2004 9:20 pm
I sense some calls to [php_man]var_export[/php_man]/[php_man]var_dump[/php_man]/[php_man]print_r[/php_man] are in order
evilmonkey
Forum Regular
Posts: 823 Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada
Post
by evilmonkey » Thu Sep 16, 2004 9:23 pm
It's wierd. It works fine if I do
It displays my words in a row, but it doesn't match them (for obvious reasons). It get through the other commands on that line for some reason, and I can't debug it because I don't really understand why they're there.
What's the condition for? (that's where the fault occurs)
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Thu Sep 16, 2004 9:35 pm
the condition is strictly there for stacking all the words into a single string for the regular expression..
alternately, you could do:
Code: Select all
<?php
$lines = file('badwords.txt');
$name = "jack";
foreach($lines as $key => $line)
{
$line = trim($line);
if(!empty($line))
$lines[$key] = preg_quote(trim($line), '#');
else
unset($lines[$key]);
}
$words = implode('|' , $lines);
if(preg_match('#(' . $words . ')#i' , $name)){
die(' bad name! ');
}
?>
evilmonkey
Forum Regular
Posts: 823 Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada
Post
by evilmonkey » Thu Sep 16, 2004 9:40 pm
Yes, that's wonderful, that works like a charm.
Thank you feyd and Wierdan for your help.