Re: preg_match - help please

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



André Medeiros wrote:
On Wed, 2005-07-27 at 12:30 -0400, John Nichel wrote:

André Medeiros wrote:

On Wed, 2005-07-27 at 11:55 -0400, John Nichel wrote:


André Medeiros wrote:


That's not very nice of you, saying that to people who try to help ;)

if( strpos( $_POST['frmName'], ' ' ) === false ) {
  // Do error handling here
} else {
  // All is OK :)
}


How does that match "Firstname Lastname" better than a regex? That will return true as long as there is at least one space, no matter what the rest of the submission is. I could submit "$^&#^&$&#& )(*!@#", or I could submit just a single space, and that would return true. strpos() has it's uses, but this isn't one of them.

There is a large number of sittuations that the regex won't work in.
Special characters aren't included (from what I can understand using my
weak regex knowlege), you could even be chinese, and it wouldn't work.

If you use strpos with trim and strlen cleverlly, you won't have to
worry about it again.

My $0.02

You can trim whitespace and check the length until the cows come home, and that still won't stop a string such as "#T*& a%@!". A regex can cover practicaly every situation for a name submission; it all depends on how deep you want to validate. strpos() has too narrow of a scope to match a complex pattern.

You can have four words to describe a first and last name... you can
have other alphabets, like arabian, chinese, etc... inserting accented
characters alone would make that a big, nasty regex, let alone
predicting ways you can describe first/last names.

And this can all be matched with a regex. Like I said, it depends on how deep you want to validate.

I'm not saying that I have the _BEST_ sollution. All I'm saying is that
there are sittuations that are out of your control, and it seems to me
this might be the easiest way out, guaranteeing that there are at least
two words.

But it doesn't even guarantee that. As shown previously, it will return true even on strings that have non-word characters in them (no matter the language). The only thing you're guaranteeing with strpos ( $string, " " ); is that the string contains a space...that's it; and basically that's what you're limited too, until you start running other functions to check/remove other parts of the string

If you find a way to fit accented characters / other alphabets there
nicelly, be my guest :)

It's really quite easy, when you look at it from the other direction. When you're trying to match a pattern that can contain just about anything and be considered valid, it might be best to check for characters that you _do not_ want in there. With that in mind, there's no reason to build a regex that can account for every possible alphabet and/or accents. Make sure it contains a space, but doesn't contain characters we don't want (such as #@%).

--
John C. Nichel
ÜberGeek
KegWorks.com
716.856.9675
john@xxxxxxxxxxxx

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux