Skip to content

Instantly share code, notes, and snippets.

@inxilpro
Created March 20, 2024 01:31
Show Gist options
  • Save inxilpro/dac45c35711e10b7763333d3bf344139 to your computer and use it in GitHub Desktop.
Save inxilpro/dac45c35711e10b7763333d3bf344139 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