I got a contact form, I need to filter some words.
I’m doing it as following:
$array = array('lorem', 'ipsum', 'ip.sum');
for($i = 0; $i < count($array); $i++)
{
if( preg_match("/".$array[$i]."/", (string) $field) )
{
return false;
}
}
I’m not a regex master, but it should be working for words like: lorem or ipsum. But it is not.
BTW. Any suggestions how to catch mispelled words, ex. i.psum, l.o.rem?
Update
Of course, I have no empty pattern, I just forgot to paste it.
Update 2
I’ve decided to got the way suggested by Daniel Vandersluis. Abnyway, I’m not able to make it working.
$field = "ipsum lorem"; // This value comes from textarea
$array = array('ipsum', 'lorem', 'ip.sum');
foreach($array as $term):
if(preg_match('/'.preg_quote($term).'/', $field)) {
return false;
}
endforeach;
Any ideas?
If I understand correctly, and you want to see if any of the words in your array are in your field, you can do something like this:
In your example, you weren’t defining any pattern to be used.
preg_quotewill take a string and make it ready to use in a regular expression (because, for example, the dot inip.sumactually has special meaning in a regular expression so it needs to be escaped if you want to search for a literal dot).As an aside, if you’d like to learn more about regular expressions, take a look at the tutorial on regular-expressions.info, it is very in depth.