PDA

View Full Version : Regular Expression Help


0ptima
02-07-2007, 10:49 PM
I have spammers signing up to my forum and entering the word "Viagra" in one of my custom fields. You can now use pattern matching in custom fields and I need help in creating a regular expression to check the field. I would like the regex to fail if the custom field contains the work Viagra. Hop someone can help.

nico_swd
02-08-2007, 10:49 AM
You could do something like this.


$badwords = array('viagra', '++++', '+++++++');

if (preg_match('/('. implode('|', array_map('preg_quote', $badwords)) .')/i', $field))
{
// Show error
}
else
{
// Submit
}

0ptima
02-08-2007, 11:54 PM
I was hoping of something more along the line of
^[0-9]{7,8}$

Here is the blurb for VB's help ...

Regular Expression (Select, Input, Radio, Textarea only)
You may require this field's contents to match a PCRE-type regular expression. For example, you could have a field that is for the user's ICQ number. Since ICQ numbers consist of only numerals, you could write a regular expression to check for non-numerals.

Example: (Do not start or end the expression with an escape character)

^[0-9]{7,8}$

If you want to allow an empty response to be given, you need to account for it within the regex:

^[0-9]{7,8}$|^$

Analogpoint
02-09-2007, 04:14 PM
If you only want to block the word 'viagra' you could try this...

This will allow anything other than the word viagra, also allows a blank field
^viagra$|^$

Do this if you want to disallow if the text *contains* the string viagra.
^.*viagra.*$|^$

0ptima
02-11-2007, 02:47 AM
I tried both regexs and they did not work.

Analogpoint
02-12-2007, 02:01 AM
Oops, you're right. The regex I gave you would require you to enter 'viagra' or a blank string. I tested this a bit, and it looks like it works, although it looks so ugly that there must be a better way to do it. I need to brush up on my regexes. Note that this matches any string that contains the string 'viagra'

^[^v]*[^i]*[^a]*[^g]*[^r]*[^a]*$|^$

0ptima
02-12-2007, 09:52 PM
Thanks, ill give it a shot.

Analogpoint
02-13-2007, 04:57 AM
Let me know if it works for you.