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!

Thanks!

Recommended Answers

All 6 Replies

its out of my mind ..............

can you give little more code?

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'];
  }else{
    $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):

"234#$RSDF$<%>
=+34DSF!@#@##$%$^
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...

see: http://www.perl.com/doc/manual/html/pod/perlre.html

specifically the modifier:

m - Treat string as multiple lines. That is, change ``^'' and ``$'' from matching at only the very start or end of the string to the start or end of any line anywhere within the string

thats a perl site.. but php ripped perls regex syntax pretty much exactly.

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.

I get it, thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.