Is there a function that lets you know if the first [0] character of a string is uppercase or lowercase? If not, what are the ways to write such a function?

    2 answers 2

    $str = 'the text to test'; if($str{0} === strtoupper($str{0})) { echo 'yepp, its uppercase'; } else{ echo 'nope, its not upper case'; } 

    original source

    • although a reference to so, it is better to publish the response code here, but to bring the link as a source. - Alex
    • This is if your string is not in multibyte encoding, yeah. - u_mulder

    Check for uppercase UTF-8 using regular expression and Unicode character properties :

     preg_match('%^\p{Lu}%u', $str) 

    Example http://sandbox.onlinephpfunctions.com/code/ad7171cbca77386187f4cf4f38a1f484a27ac506

     <?php function first($str) { if(preg_match('%^\p{Lu}%u', $str)) { var_dump($str . ' - прописная'); } else { var_dump($str . ' - строчная'); } } first('Привет мир!'); first('привет мир!'); first('the text to test'); first('The text to test'); first('123'); first(''); 

    Result

     string(41) "Привет мир! - прописная" string(39) "привет мир! - строчная" string(35) "the text to test - строчная" string(37) "The text to test - прописная" string(22) "123 - строчная" string(19) " - строчная" 
    • one
      Please correct the link, link the code from the previous answer - Alex