Skip to content

Commit

Permalink
[PHP 8.4] Add mb_ucfirst() and mb_lcfirst()
Browse files Browse the repository at this point in the history
  • Loading branch information
zonuexe committed Mar 30, 2024
1 parent df4793d commit 26909f0
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Polyfills are provided for:
- the `str_increment` and `str_decrement` functions introduced in PHP 8.3;
- the `Date*Exception/Error` classes introduced in PHP 8.3;
- the `SQLite3Exception` class introduced in PHP 8.3;
- the `mb_ucfirst` and `mb_lcfirst` functions introduced in PHP 8.4;

It is strongly recommended to upgrade your PHP version and/or install the missing
extensions whenever possible. This polyfill should be used only when there is no
Expand Down
12 changes: 12 additions & 0 deletions src/Mbstring/Mbstring.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
* - mb_strstr - Finds first occurrence of a string within another
* - mb_strwidth - Return width of string
* - mb_substr_count - Count the number of substring occurrences
* - mb_ucfirst - Make a string's first character uppercase
* - mb_lcfirst - Make a string's first character lowercase
*
* Not implemented:
* - mb_convert_kana - Convert "kana" one from another ("zen-kaku", "han-kaku" and more)
Expand Down Expand Up @@ -871,6 +873,16 @@ public static function mb_str_pad(string $string, int $length, string $pad_strin
}
}

public static function mb_ucfirst(string $string, ?string $encoding = null): string
{
return \mb_convert_case(\mb_substr($string, 0, 1, $encoding), \MB_CASE_TITLE, $encoding) . \mb_substr($string, 1, null, $encoding);
}

public static function mb_lcfirst(string $string, ?string $encoding = null): string
{
return \mb_strtolower(\mb_substr($string, 0, 1, $encoding), $encoding) . \mb_substr($string, 1, null, $encoding);
}

private static function getSubpart($pos, $part, $haystack, $encoding)
{
if (false === $pos) {
Expand Down
7 changes: 7 additions & 0 deletions src/Mbstring/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ function mb_str_split($string, $length = 1, $encoding = null) { return p\Mbstrin
function mb_str_pad(string $string, int $length, string $pad_string = ' ', int $pad_type = STR_PAD_RIGHT, ?string $encoding = null): string { return p\Mbstring::mb_str_pad($string, $length, $pad_string, $pad_type, $encoding); }
}

if (!function_exists('mb_ucfirst')) {
function mb_ucfirst(string $string, ?string $encoding = null): string { return p\Mbstring::mb_ucfirst($string, $encoding); }
}
if (!function_exists('mb_lcfirst')) {
function mb_lcfirst(string $string, ?string $encoding = null): string { return p\Mbstring::mb_lcfirst($string, $encoding); }
}

if (extension_loaded('mbstring')) {
return;
}
Expand Down
8 changes: 8 additions & 0 deletions src/Mbstring/bootstrap80.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ function mb_str_split(?string $string, ?int $length = 1, ?string $encoding = nul
function mb_str_pad(string $string, int $length, string $pad_string = ' ', int $pad_type = STR_PAD_RIGHT, ?string $encoding = null): string { return p\Mbstring::mb_str_pad($string, $length, $pad_string, $pad_type, $encoding); }
}

if (!function_exists('mb_ucfirst')) {
function mb_ucfirst(string $string, ?string $encoding = null): string { return p\Mbstring::mb_ucfirst($string, $encoding); }
}

if (!function_exists('mb_lcfirst')) {
function mb_lcfirst(string $string, ?string $encoding = null): string { return p\Mbstring::mb_lcfirst($string, $encoding); }
}

if (extension_loaded('mbstring')) {
return;
}
Expand Down
9 changes: 9 additions & 0 deletions src/Php84/Php84.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,13 @@
*/
final class Php84
{
public static function mb_ucfirst(string $string, ?string $encoding = null): string
{
return \mb_convert_case(\mb_substr($string, 0, 1, $encoding), \MB_CASE_TITLE, $encoding) . \mb_substr($string, 1, null, $encoding);
}

public static function mb_lcfirst(string $string, ?string $encoding = null): string
{
return \mb_strtolower(\mb_substr($string, 0, 1, $encoding), $encoding) . \mb_substr($string, 1, null, $encoding);
}
}
3 changes: 3 additions & 0 deletions src/Php84/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ Symfony Polyfill / Php84

This component provides features added to PHP 8.4 core:

- [`mb_lcfirst`](https://wiki.php.net/rfc/mb_ucfirst)
- [`mb_ucfirst`](https://wiki.php.net/rfc/mb_ucfirst)

More information can be found in the
[main Polyfill README](https://github.com/symfony/polyfill/blob/main/README.md).

Expand Down
8 changes: 8 additions & 0 deletions src/Php84/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,11 @@
if (\PHP_VERSION_ID >= 80400) {
return;
}

if (!function_exists('mb_ucfirst') && function_exists('mb_substr')) {
function mb_ucfirst(string $string, ?string $encoding = null): string { return p\Php84::mb_ucfirst($string, $encoding); }
}

if (!function_exists('mb_lcfirst') && function_exists('mb_substr')) {
function mb_lcfirst(string $string, ?string $encoding = null): string { return p\Php84::mb_lcfirst($string, $encoding); }
}

0 comments on commit 26909f0

Please sign in to comment.