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?
The ctype_upper() function in PHP check for uppercase character(s). It returns TRUE if every character in text is an uppercase letter in the current locale.
Find the ASCII value of the character. If the ASCII value of the character is between 65 and 90, print "Upper". If the ASCII value of the character is between 97 and 122, print "Lower". If the ASCII value of the character is between 48 and 57, print "Number".
ctype_lower() function in PHP The ctype_lower() function check for lowercase character(s). It returns TRUE if every character in text is a lowercase letter in the current locale.
To check if a letter in a string is uppercase or lowercase use the toUpperCase() method to convert the letter to uppercase and compare it to itself. If the comparison returns true , then the letter is uppercase, otherwise it's lowercase. Copied!
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.
Working Demo Online
Use ctype_upper
for check upper case:
$a = array("Word", "word", "wOrd"); foreach($a as $w) { if(ctype_upper($w{0})) { print $w; } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With