Skip to content Skip to sidebar Skip to footer

How To Check If Input Value Begins With An Uppercase/ Or If It Has Lowercases/ Or If It's All Uppercase In Php?

I'm quite new to PHP and don't have as much experience with is as I do with other languages. I have the following form, and what I would like to do is to check if the firstname fie

Solution 1:

The answer is surprisingly simple. You can use a combination of strtoupper, strtolower and substr with strict comparing.

$firstLetterUppercase = 'A_word';

if(substr($firstLetterUppercase, 0, 1) === strtoupper(substr($firstLetterUppercase, 0, 1))) {
    // first letter is upper case
}

$nickNameLowerCase = 'nickname';

if($nickNameLowerCase === strtolower($nickNameLowerCase)) {
    // it's lower case
}

$lastName = 'ALL UPPER CASE';

if($lastName === strtoupper($lastName)) {
    // it's upper case
}

If you mix and match them you can get just about anything done.

Do note that there's no difference between loose and strict compare here, but it's always nice to use strict.

Solution 2:

  1. To check the first letter with upper case try below

    preg_match("/^[A-Z]/", 'Bamboozler');// This will returntrueif first letter is upper case
  2. To check all the character of a string lowercase, you can use ctype_lower() function

    ctype_lower('bamboozler');// This will returntrue if all the characterare lower cases.
    
  3. To check all the character of a string uppercase, you can use ctype_upper() function

    ctype_upper('BOOM');// This will return true if all the character is upper cases.

Solution 3:

A general case check function.

No need to use preg_match() or substr(). You can directly access the first character using the string index method of {0}. If you wanted the second character, use {1}.

functioncheckFirstLetter($str,$upper=true) {
  if ($upper) {
    return ($str{0} == strtoupper($str{0}));
  } else {
    return ($str{0} == strtolower($str{0}));
  }
}

$test[] = array("str" => "test", "case" => true);
$test[] = array("str" => "Test", "case" => true);
$test[] = array("str" => "test", "case" => false);
$test[] = array("str" => "Test", "case" => false);
$test[] = array("str" => "123", "case" => true);
$test[] = array("str" => "123", "case" => false);
$test[] = array("str" => "!@#", "case" => true);
$test[] = array("str" => "!@#", "case" => false);

foreach($testas$k => $v) {
  echo'str:['.$v['str'].'] 
    upper:['.$v['case'].'] 
    result:['.checkFirstLetter($v['str'],$v['case']).']<br />';
}

And the results:

str:[test] upper:[1] result:[]
str:[Test] upper:[1] result:[1]
str:[test] upper:[] result:[1]
str:[Test] upper:[] result:[]
str:[123] upper:[1] result:[1]
str:[123] upper:[] result:[1]
str:[!@#] upper:[1] result:[1]str:[!@#] upper:[] result:[1]

Edit: I really like the ctype_upper($str) and ctype_lower($str) option, but you will need to have that extension installed and I do not. If you do have this extension, you could do:

if (ctype_upper($str{0})) {
  // is upper first
}

There is also ucfirst() and ucwords() - both may help based on your question if you want to force a syntax standard on user input without the need to error report.

Solution 4:

This will return true if first character is uppercase. while $str is the string you want to check.

preg_match("/^[A-Z]/", $str );

you can do like this if you need to convert whole string to uppercase,

if( preg_match("/^[A-Z]/", $str ) ){
$str = strtoupper("$str");
echo$str;
}

similarly you can convert string to lowercase using strtolower()

Post a Comment for "How To Check If Input Value Begins With An Uppercase/ Or If It Has Lowercases/ Or If It's All Uppercase In Php?"