Text register
Upper and lower case
You can make letters uppercase or lowercase using the mb_strtoupper() and mb_strtolower() functions.
$text = 'Text';
$text = mb_strtoupper($text, 'UTF-8'); // TEXT
$text = mb_strtolower($text, 'UTF-8'); // text
The strtoupper() and strtolower() functions are not recommended for use because they do not understand Unicode.
The first letter is capitalized
You can capitalize the first letter using the mb_ucfirst() function.
mb_ucfirst('text'); // Text (since PHP 8.4)
The ucfirst() function is not recommended because it does not understand Unicode. Prior to PHP 8.4, a separate function was created:
if (!function_exists('mb_ucfirst')) {
function mb_ucfirst($string, $enc = 'UTF-8') {
return mb_strtoupper(mb_substr($string, 0, 1, $enc), $enc).mb_substr($string, 1, mb_strlen($string, $enc), $enc);
}
}
echo mb_ucfirst('zend'); // Zend
echo mb_ucfirst('зенд'); // Зенд
Capitalize every word
You can capitalize each word using the mb_convert_case() function.
mb_convert_case('hello world', MB_CASE_TITLE); // Hello World
There is also a ucwords() function, but it does not understand Unicode.
Check text for upper or lower case
To check whether the text is in upper or lower case, separate functions are created.
function isUpper($text) {
return $text == mb_strtoupper($text);
}
function isLower($text) {
return $text == mb_strtolower($text);
}
isUpper('PHP'); // true
isUpper('php'); // false
isLower('php'); // true
isLower('PHP'); // false
PHP has ctype_upper(), ctype_lower(), IntlChar::islower() and IntlChar::isupper() functions that check the case of text, but they do not understand Unicode.
Поддержать автора
Все статьи создаются и поддерживаются одним человеком,
если вам нравится контент, вы можете поддержать автора сайта
