Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if letter is upper or lower in PHP?

Tags:

string

php

utf-8

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?

like image 688
Tom Smykowski Avatar asked May 11 '10 22:05

Tom Smykowski


People also ask

How do I check if a letter is uppercase in PHP?

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.

How do you check if a character is upper or lower?

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".

How do you check if a string is in lowercase in PHP?

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.

How do you check if a character in a string is uppercase?

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!


2 Answers

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

like image 193
Artefacto Avatar answered Sep 18 '22 17:09

Artefacto


Use ctype_upper for check upper case:

$a = array("Word", "word", "wOrd");  foreach($a as $w) {     if(ctype_upper($w{0}))     {         print $w;     } } 
like image 20
Eugen Avatar answered Sep 20 '22 17:09

Eugen