View Full Version : Help with regular expression

04-12-2003, 07:13 AM
Can't seem to get this to work.

I have an array or words which have to be filtered out.

Now for each word it can only be filtered if there is a space or a non-word character at the start and at the end.

I had a go at it but nothing good came out of it. heh,.

Oh also the variable name is $bw so it'd be like (\040|\W($bw)\040|\W) or something?

04-12-2003, 07:54 AM
I'm warning you, I know nothing about reg exp's. Using my book a bit of common sense I cam up with the following script:


$badwords = Array();
$badwords[] = "hate";
$badwords[] = "suck";

$usrinput = "Hello, my name is Joe. I hate regular expressions. They suck. Testhate , will it replace that <-- ? I hope not! Regular exp's suck, they really do.";

foreach ( $badwords as $bg ) {
$usrinput = ereg_replace("[^a-zA-Z0-9]".$bg."[^a-zA-Z0-9]", "xxx", $usrinput);

print $usrinput;


Only problem is that it matches and replaces the non-alphanumeric character before and after the found expressions. I don't know how to make it so it checks for something, but doesn't replace it. Hopefully it is something you can try to work with.

It's not that I trully dislike regular expressions, it's that I don't have a good understanding of them and human nature dictates that we are afraid/show hatred towards that which we do not know, or know little about. ( ignorance == "fear" )

04-12-2003, 08:00 AM
Thanks x_goose_x! You're a champ. :D

04-12-2003, 08:05 AM
Don't thank me, thank the cheapest book I could find, "SAMS teach yours..."

04-12-2003, 08:39 AM
lol, might get that book. hrmm..