Capitalization

Discussion in 'PHP' started by Kennedy, Mar 3, 2008.

  1. #1
    Is there any way to see if a string has a capital letter anywhere in it?

    example:

    $str = "Has a capital letter."

    if ($str has a capital letter) {
    do this;
    }
     
    Kennedy, Mar 3, 2008 IP
  2. AsHinE

    AsHinE Well-Known Member

    Messages:
    240
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    138
    #2
    I don't think it is effective method, but:
    
    $str = "Has a capital letter.";
    if ($str!=strtolower($str)){
      echo "+";
    }
    
    PHP:
     
    AsHinE, Mar 3, 2008 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    I think you can do a preg_match for the first letter in the string. I'm not sure on the exact regex, but pcre allows you to match by upper or lower case letters independently [A-Z] or [a-z].

    Something like;

    
    
    if(preg_match('/[A-Z]/',trim($string['0']))){
    
    //uppercase letter
    
    } else {
    
    //Not uppercase
    
    }
    
    
    PHP:
    The above post would work, but would return false if there was any uppercase letter, not just the first letter.
     
    jestep, Mar 3, 2008 IP