What's the best way to check if a string is all uppercase? I tried ctype_upper but it returns false if there is any whitespace in the string. I need to insert the string into a database if it is not all uppercase. I was doing this: if (!ctype_upper($title) { // insert into db } else { // do nothing } PHP: Any ideas?
I tried both methods but a 'STRING LIKE THIS' still slips through to the db. I currently have this: if (preg_match('~^[A-Z\s]$~', $title[i])) { $title[$i] = false; } else { //Insert into DB } PHP: It's $title because it's going through a loop checking 10 titles. Any more ideas?
Basically if the string is all UPPERCASE I want to discard it and not insert it into the db. I know I could modify it so it's not all uppercase but then some strings that have some capitals in the string eg: 'Title Of DVD' would also be modified. Hope that makes sense. Thanks.
function is_uppercase($string, $result = false) { $len = strlen($string) - 1; for ($i = 0; $i <= $len; $i++) { if (ctype_alpha($string{$i}) && ctype_upper($string{$i}) === false) { $result = true; } } return $result; } // eg $tests = array('asfasf3', 'asfHF', 'asf asf', 'AF AS F', 'ASGAHSG', 'AGHh'); foreach ($tests as $k) { echo $k , ': ' , (is_uppercase($k)) ? 'allow it, not all uppercase' : 'all uppercase', "<br />"; } PHP: If it returns 'true', then insert it, otherwise discard.
The below version is faster and safer, because it will work if $title[$i] has other characters like numbers or punctuations as well. if ($title[$i] != strtoupper($title[$i])) { // insert to db } PHP:
Thanks to everyone for your help. phper, I ended up using your solution. It appears to be working well. I could have sworn I tried something like that before and it didn't work. Ah well, thanks!