8000 [String]  fix handling of punctuation characters in snake() by xabbuh · Pull Request #57615 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[String]  fix handling of punctuation characters in snake() #57615

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix handling of punctuation characters in snake()
  • Loading branch information
xabbuh committed Jul 1, 2024
commit a232852094d14995359d6832ee4b5120591e89f9
4 changes: 3 additions & 1 deletion src/Symfony/Component/String/AbstractUnicodeString.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,9 @@ public function reverse(): parent
public function snake(): parent
{
$str = clone $this;
$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'));
$str->string = preg_replace('/[^\pL0-9_ ]++/u', '', $str->string);
$str->string = mb_strtolower(preg_replace(['/(\p{Lu}+)(\p{Lu}\p{Ll})/u', '/([\p{Ll}0-9])(\p{Lu})/u'], '\1 \2', $str->string), 'UTF-8');
$str->string = preg_replace('/[ _]+/', '_', $str->string);

return $str;
}
Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Component/String/ByteString.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,9 @@ public function slice(int $start = 0, ?int $length = null): parent
public function snake(): parent
{
$str = clone $this;
$str->string = preg_replace('/[ _]+/', '_', strtolower(preg_replace(['/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'], '\1 \2', $str->string)));
$str->string = preg_replace('/[^a-zA-Z0-9\x7f-\xff_ ]++/', '', $str->string);
$str->string = strtolower(preg_replace(['/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'], '\1 \2', $str->string));
$str->string = preg_replace('/[ _]+/', '_', $str->string);

return $str;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,7 @@ public static function provideSnake()
['symfony_is_great', 'symfony is great'],
['symfony_is_great', 'SYMFONY IS GREAT'],
['symfony_is_great', 'SYMFONY _ IS _ GREAT'],
['symfony_is_great', 'Symfony is great.'],
];
}

Expand Down
0