note 87844 deleted from function.preg-match by thiago

From: Date: Fri, 26 Dec 2008 12:05:09 +0000
Subject: note 87844 deleted from function.preg-match by thiago
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-148522@lists.php.net to get a copy of this message
Note Submitter: Pete Reason: in docs ---- Can you believe it, right after I posted my last comment I figured it out, usually happens. Help to any newbies that struggle with it: using: preg_match(/^[a-zA-Z]+[^0-9]/', $string); means match characters a up to z at beggining and through the string, dont match 0 to 9, but for some reason it will ignore this 'dont match' 0-9 because there is no ending expression. So as long as there are characters a-z even if there are digits it will still return true. To get past this, you must declare what you want matching at the end of the string i.e: preg_match(/^[a-zA-Z]+[^0-9][a-zA-Z]+$/', $string); Notice that extra expression at the end with the $ sign stating that last expression must match at the end of the string. So now it reads, if first character is a-z and there is not 0-9 and end character is a-z return true.

« previous php.notes (#148522) next »