RE: [PHP] Weird preg problem

From: Date: Thu, 04 Jul 2002 06:52:59 +0000
Subject: RE: [PHP] Weird preg problem
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-105380@lists.php.net to get a copy of this message
Thanks Miguel it works now! $str = "abcA"; if (preg_match('/^[a-z]*$/', $str)) echo "The string '$str' contains only lower-case letters."; -----Original Message----- From: Miguel Cruz [mailto:mnc@stoic.net] Sent: July 4, 2002 2:29 AM To: SP Cc: php-general@lists.php.net Subject: Re: [PHP] Weird preg problem On Thu, 4 Jul 2002, SP wrote: > Hi I am trying to check if a word contains only capital letters. For > some reason it's not working. The below example is checking the word > "weird" to see if it's all capital letters but it's saying it's > matches. > I've tried checking for only lower case letters with "WEIRD" and it's > not working there too. What am I doing wrong? > > > $pattern = "A-Z"; > $regexp = '/^['.$pattern.']*$/i'; > if( preg_match($regexp, "weird") ) > echo "matches"; > else > echo "does not match"; Easiest approach: if ($str != strtoupper($str)) echo 'String contains lower-case letters.'; Using preg: if (preg_match('/[a-z]/', $str)) echo 'String contains lower-case letters.'; If you don't want any other characters to appear, ONLY capital letters (your question was a little ambiguous to me): if (preg_match('/[^A-Z]/', $str)) echo 'String contains something other than capital letters.'; miguel

« previous php.general (#105380) next »