[SOLVED] Little regex help

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
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

[SOLVED] Little regex help

Post by evilmonkey »

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! :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

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! ');

?>
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

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!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

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)
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

feyd mistyped empty(... part. that should be:

Code: Select all

//....
$words .= (empty($words) ? '' : '|') . preg_quote(trim($line), '#');
//....
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

:oops: ;)

thats what I get for typing real fast :P
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

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:

Code: Select all

word1
word2
word3
...
word55
I don't know how happy people will be with me posting the actual words, so I just replaced them. :P

Thanks!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

maybe you have an empty line in the file... check a rendering of $words
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

Hmm, echo $words; comes up blank...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

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 ;)
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

It's wierd. It works fine if I do

Code: Select all

$words.=preg_quote(trim($line))?>
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)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

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! ');
}
?>
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

Yes, that's wonderful, that works like a charm.

Thank you feyd and Wierdan for your help. :mrgreen:
Post Reply