tags:

views:

323

answers:

2

I have texts in UTF-8 with diacritic characters also, and would like to check if first letter of this text is upper case or lower case. How to do this?

A: 

Tried ?

$str = 'the text to test';
if($str{0} === strtoupper($str{0})) {
   echo 'yepp, its uppercase';
}
else{
   echo 'nope, its not upper case';
}
PHP_Jedi
what does mean $str{0}? I don't know this notation...
tomaszs
+4  A: 
function starts_with_upper($str) {
    $chr = mb_substr ($str, 0, 1, "UTF-8");
    return mb_strtolower($chr, "UTF-8") != $chr;
}

Note that mb_substr is necessary to correctly isolate the first character.

Artefacto
thank you, your answer is right. You can not use $str[0] on UTF-8 string.
tomaszs
Doesn't always work. There are Unicode characters that are capital letters (i.e., category Lu) but don't have a lowercase mapping. Mostly, the mathematical bold/italic/double-struck letters.
dan04
Artefacto
@dan04 How this function will handle this situation?
tomaszs