Skip to content

Commit 4e57b84

Browse files
bug #57594 [String] Normalize underscores in snake() (xabbuh)
This PR was merged into the 5.4 branch. Discussion ---------- [String] Normalize underscores in snake() | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #57497 (comment) | License | MIT Commits ------- 0e6cd07 normalize underscores in snake()
2 parents 19b4bf0 + 0e6cd07 commit 4e57b84

File tree

3 files changed

+5
-2
lines changed

3 files changed

+5
-2
lines changed

src/Symfony/Component/String/AbstractUnicodeString.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ public function reverse(): parent
367367
public function snake(): parent
368368
{
369369
$str = clone $this;
370-
$str->string = str_replace(' ', '_', mb_strtolower(preg_replace(['/(\p{Lu}+)(\p{Lu}\p{Ll})/u', '/([\p{Ll}0-9])(\p{Lu})/u'], '\1 \2', $str->string), 'UTF-8'));
370+
$str->string = preg_replace('/[ _]+/', '_', mb_strtolower(preg_replace(['/(\p{Lu}+)(\p{Lu}\p{Ll})/u', '/([\p{Ll}0-9])(\p{Lu})/u'], '\1 \2', $str->string), 'UTF-8'));
371371

372372
return $str;
373373
}

src/Symfony/Component/String/ByteString.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ public function slice(int $start = 0, ?int $length = null): parent
367367
public function snake(): parent
368368
{
369369
$str = clone $this;
370-
$str->string = str_replace(' ', '_', strtolower(preg_replace(['/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'], '\1 \2', $str->string)));
370+
$str->string = preg_replace('/[ _]+/', '_', strtolower(preg_replace(['/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'], '\1 \2', $str->string)));
371371

372372
return $str;
373373
}

src/Symfony/Component/String/Tests/AbstractAsciiTestCase.php

+3
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,9 @@ public static function provideSnake()
10791079
['symfony', 'SYMFONY'],
10801080
['symfony_is_great', 'SYMFONY IS GREAT'],
10811081
['symfony_is_great', 'SYMFONY_IS_GREAT'],
1082+
['symfony_is_great', 'symfony is great'],
1083+
['symfony_is_great', 'SYMFONY IS GREAT'],
1084+
['symfony_is_great', 'SYMFONY _ IS _ GREAT'],
10821085
];
10831086
}
10841087

0 commit comments

Comments
 (0)