Skip to content

Instantly share code, notes, and snippets.

@whoami15
Forked from inxilpro/str-extreme-snake.php
Created March 20, 2024 23:28
Show Gist options
  • Save whoami15/6ac1ee187a8d302c4a577b6a349803a4 to your computer and use it in GitHub Desktop.
Save whoami15/6ac1ee187a8d302c4a577b6a349803a4 to your computer and use it in GitHub Desktop.
<?php
\Illuminate\Support\Str::macro('extremeSnake', function($value, $delimiter = '_') {
$pattern = <<<'REGEXP'
/
(?<!^) # don't match the beginning of a string
(
(?<=[^\p{Lu}])[\p{Lu}\p{M}]+(?=\p{M}?[^\p{Ll}]\p{M}?\p{L}) # string of upper-case (like an abbreviation)
| (?<=\p{Lu}{2})[\p{Lu}\p{M}](?=\p{M}?\p{Ll}) # the final upper-case in a sequence
| (?<=[^\p{Lu}])[\p{Lu}\p{M}](?=\p{M}?\p{Ll}) # first upper-case in a capitalized sequence
)
/ux
REGEXP;
$value = preg_replace($pattern, ' $1', trim($value));
return preg_replace('/[^\p{L}0-9]+/u', $delimiter, Str::lower($value));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment