Hello! This is my first post here. :)

I'm trying to use this regexp pattern to filter form input (the message body) that will be emailed to me from a "contact us" form.

The pattern is (i removed the punctuation and other special chars for the sake of ease):

$clean['match'] = '/^[A-Za-z0-9]+$/m';

But it seems to check only the last line. When i use it in a single line mode (i use the same pattern for the subject - without the m) it works fine. I've been trying to work it out but i can't find what's going on. Everything that i've read says that using "m" will check over multiple lines blablabla but obviously i'm missing something small here cause it can't be that hard to accomplish this.

Can someone help me please!


9 Years
Discussion Span
Last Post by Leandro-AL

Well i had this thing:

$trimmed = array_map('trim', $_POST);  
  $clean['match'] = '/^[A-Za-z0-9]+$/m';
  if (preg_match($clean['match'], stripslashes($trimmed['message']))){
    $clean['message'] = $trimmed['message'];
    $errors[] = 'That is not a valid message.';

But for some reason (maybe i'm doing it wrong) it would only check the last line of a block of text. As long as the last line matched the pattern, the rest of the lines could contain anything. For example this would mach (although it shouldn't):

last line"

In the end i used:

$clean['match'] = '/^[A-Za-z0-9\s]+$/';

and it worked however i still dont get it why the \m didn't work...


So if the pattern matches at least one line, all of the string will flag true, even if the other lines don't match?


Yes. The "m" flag is more useful combined with either global match or global replace, since then you can perform one operation per line. For the test you're doing, it's gonna return true if any of the lines in the input match.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.