Skip to content

Commit 52b2b26

Browse files
[9.x] Add Str::lcfirst to support non-ascii characters (vs php's core lcfirst fct) and add DX consistency with ucfirst wrapper (#41384)
* Add the lcfirst function to the Str class * formatting Co-authored-by: Taylor Otwell <[email protected]>
1 parent b7625b3 commit 52b2b26

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

src/Illuminate/Support/Str.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,17 @@ public static function swap(array $map, $subject)
987987
return strtr($subject, $map);
988988
}
989989

990+
/**
991+
* Make a string's first character lowercase.
992+
*
993+
* @param string $string
994+
* @return string
995+
*/
996+
public static function lcfirst($string)
997+
{
998+
return static::lower(static::substr($string, 0, 1)).static::substr($string, 1);
999+
}
1000+
9901001
/**
9911002
* Make a string's first character uppercase.
9921003
*

src/Illuminate/Support/Stringable.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,16 @@ public function rtrim($characters = null)
801801
return new static(rtrim(...array_merge([$this->value], func_get_args())));
802802
}
803803

804+
/**
805+
* Make a string's first character lowercase.
806+
*
807+
* @return static
808+
*/
809+
public function lcfirst()
810+
{
811+
return new static(Str::lcfirst($this->value));
812+
}
813+
804814
/**
805815
* Make a string's first character uppercase.
806816
*

tests/Support/SupportStrTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,14 @@ public function testSubstrReplace()
638638
$this->assertSame('Laravel – The PHP Framework for Web Artisans', Str::substrReplace('Laravel Framework', '– The PHP Framework for Web Artisans', 8));
639639
}
640640

641+
public function testLcfirst()
642+
{
643+
$this->assertSame('laravel', Str::lcfirst('Laravel'));
644+
$this->assertSame('laravel framework', Str::lcfirst('Laravel framework'));
645+
$this->assertSame('мама', Str::lcfirst('Мама'));
646+
$this->assertSame('мама мыла раму', Str::lcfirst('Мама мыла раму'));
647+
}
648+
641649
public function testUcfirst()
642650
{
643651
$this->assertSame('Laravel', Str::ucfirst('laravel'));

0 commit comments

Comments
 (0)