Change Multibyte String Case using mb_ucfirst and mb_lcfirst in PHP 8.4

Change Multibyte String Case using mb_ucfirst and mb_lcfirst in PHP 8.4

PHP provides the ucfirst and lcfirst functions to convert the first character of a given string to uppercase or lowercase. These functions are particularly useful for formatting titles and labels. However, the ucfirst and lcfirst functions do not support multibyte characters, which can cause problems when working with languages that use multibyte encodings like UTF-8.

Since PHP 8.4, the mb_ucfirst and mb_lcfirst functions has become available. These functions offer a multibyte-safe way to change the case of the first character to uppercase or lowercase in a given string.

<?php

echo mb_ucfirst('ärmel'); // Ärmel
echo mb_lcfirst('Ärmel'); // ärmel

Leave a Comment

Cancel reply

Your email address will not be published.