PHP does not have a multibyte equivalent of ucfirst, lcfirst functions. It is possible to get close enough behavior below:
function mb_ucfirst(string $str, ?string $encoding = null): string { return mb_convert_case(mb_substr($str, 0, 1, $encoding), MB_CASE_TITLE, $encoding) . mb_substr($str, 1, null, $encoding); }
function mb_lcfirst(string $str, ?string $encoding = null): string { return mb_strtolower(mb_substr($str, 0, 1, $encoding), $encoding) . mb_substr($str, 1, null, $encoding); }
However adding a pre-built functions to do this will implobe the readability and clarify of PHP code. And it will standardize how it is done as it can be tricky.
Add mb_ucfirst function, mb_lcfirst function.
function mb_ucfirst(string $string, ?string $encoding = null): string
The first character in mb_ucfirst uses Unicode title case.
function mb_lcfirst(string $string, ?string $encoding = null): string
From what I've researched with Unicode, it may not behave as expected in some languages. In that case, please deal with it in userland.
For example, In Vietnamese, the first letter is not always capitalized.
Another example, In Georgian should uses title case.
Correct case.
This could break a function existing in userland with the same name.
next PHP 8.x
To SAPIs Will add the aforementioned functions to all PHP environments.
Adds mb_ucfirst(), mb_lcfirst() to the mbstring extension.
No effect.
No new constants.
No changed php.ini settings.
This section details areas where the feature might be improved in future, but that are not currently proposed in this RFC.
Keep this updated with features that were discussed on the mail lists.