Check string is all uppercase

Discussion in 'PHP' started by joshm, Nov 18, 2007.

  1. #1
    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?
     
    joshm, Nov 18, 2007 IP
  2. mahmood

    mahmood Guest

    Messages:
    1,228
    Likes Received:
    43
    Best Answers:
    0
    Trophy Points:
    0
    #2
    How about this?

    if($title == strtoupper($title)){
    //do something
    } else{
    //do something else
    }
     
    mahmood, Nov 18, 2007 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    
    if (preg_match('~^[A-Z\s]$~', $title))
    
    PHP:
     
    nico_swd, Nov 18, 2007 IP
  4. joshm

    joshm Peon

    Messages:
    59
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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?
     
    joshm, Nov 18, 2007 IP
  5. bobb1589

    bobb1589 Peon

    Messages:
    289
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #5
    why not just strtolower()?
     
    bobb1589, Nov 18, 2007 IP
  6. itliberty

    itliberty Peon

    Messages:
    1,173
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #6
    clarify, do you need to check or do you need to change it before inserting..
     
    itliberty, Nov 18, 2007 IP
  7. joshm

    joshm Peon

    Messages:
    59
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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.
     
    joshm, Nov 18, 2007 IP
  8. decepti0n

    decepti0n Peon

    Messages:
    519
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #8
    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.
     
    decepti0n, Nov 18, 2007 IP
  9. bobb1589

    bobb1589 Peon

    Messages:
    289
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #9
    why not just strtolower and then ucfirst ?
     
    bobb1589, Nov 19, 2007 IP
  10. rustem

    rustem Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Try checking after removing all whitespaces.
     
    rustem, Nov 19, 2007 IP
  11. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #11
    I forgot a plus sign here.
    
    if (preg_match('~^[A-Z\s]+$~', $title))
    
    PHP:
     
    nico_swd, Nov 19, 2007 IP
  12. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #12
    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:
     
    phper, Nov 19, 2007 IP
  13. joshm

    joshm Peon

    Messages:
    59
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    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! :)
     
    joshm, Nov 24, 2007 IP