PDA

View Full Version : EXTREME Regular Expression Fustration


Michael Morris
02-21-2005, 03:50 AM
This is pissing me the Hell off.

function spamkill($post)
{
eval('$blacklist = "' . fetch_template('mtblacklist') . '";');
$spamlist = explode("\n", $blacklist);

foreach ($spamlist as $spam)
{
// Chop off comment text at the end of some lines as necessary
if (strstr($spam, '#'))
{
$spam = trim(substr($spam, 0, strpos($spam,"#") - 1));
}

if (!strstr($spam, '#') AND strlen($spam) != 0)
{
preg_match("#$spam#siU", $post['message'], $matches);

echo $matches[0] . "<br />";
}
}

die;
return false;
}

Template mtblacklist contains this file

http://www.jayallen.org/comment_spam/blacklist.txt

The explode statement works, breaking that list into a line by line array.

The first if statement works, lopping off the comment text.

The preg_match isn't working. Neither will ereg, eregi, strstr or stristr. In fact, NOTHING IS WORKING!!! Even when the post is an EXACTLY COPIED LINE from the blacklist a if($post['message'] == $spam) statement DOESN'T WORK. I'm about to punch a hole in my laptop monitor here. Help.

Marco van Herwaarden
02-21-2005, 06:10 AM
Your pattern should AFAIK (i really hate those preg functions ;)) be something like "/....../", i don't see the / delimiter in your example.

So i would say try:
preg_match("/$spam/siU", $post['message'], $matches);

Don't know what you try with the "#".

Michael Morris
02-21-2005, 08:10 AM
I figured it out after another three hours of smashing - but thanks. I'll be using the eregi function.