RE: [PHP] Pattern Matching

From: Date: Fri, 23 May 2003 16:54:54 +0000
Subject: RE: [PHP] Pattern Matching
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-148799@lists.php.net to get a copy of this message
> Difficult to learn new tricks when you're used to doing something one way, > but im applying my mind. Never the less, > > Im trying to pattern match to find empty fields. > > Im using the ereg() function. In perl, you just call it. > > snipit > if ( ereg ( " ", $value)) { > > echo "'$key' Missing input "; > } > > The results : returns false every time. Except for the Submit button, > then it returns true *sigh*.... > > I know that it has to do with what Im searching for and that is basically > just a " " <- blank space. Why not just use empty()? foreach($_POST as $key=>$value) { if(empty($value)) { echo "$key: Missing Input"; } } Don't use regular expressions when you don't have to. Yes, they are nice and very useful, but you'll be surprised that a lot of the time having more code and using simple string functions will be faster than a regex one-liner. Always benchmark your code. As for your problem, I suspect it's because you're searching for a space instead of an empty string. "" vs. " ". An empty form field is going to be "" (for text fields) or not even set if it's a checkbox/radio button. Hope that helps. ---John W. Holmes... PHP Architect - A monthly magazine for PHP Professionals. Get your copy today. http://www.phparch.com/

« previous php.general (#148799) next »