<?php
// phpcs:ignoreFile
$separator = '-';
$subjects = [
'SomeCamelCase' => 'Some-Camel-Case',
'Some12With5Numbers' => 'Some-12-With-5-Numbers',
'SomePDFInText' => 'Some-PDF-In-Text',
'123LeadingNumbers' => '123-Leading-Numbers',
'ItIs2016' => 'It-Is-2016',
'What-If' => 'What-If',
'ASingleLetterB' => 'A-Single-Letter-B',
'some_snake_case' => 'some_snake_case',
'Title_Snake_Case' => 'Title-_-Snake-_-Case',
'lower-with-dash' => 'lowerwithdash',
'FFS!' => 'FFS-!',
'WithA😃' => 'With-A-😃',
'PDF123' => 'PDF-123',
'EmojiInThe🤞Middle' => 'Emoji-In-The-🤞-Middle',
'12345' => '12345',
'123A' => '123-A',
'A123' => 'A-123',
'War&Peace' => 'War-&-Peace',
'lowerThenTitleCase' => 'lower-Then-Title-Case',
'123lower' => '123-lower',
'lower123' => 'lower-123',
'ItIsÜber' => 'It-Is-Über',
'SømeThing' => 'Søme-Thing',
];
foreach ($subjects as $subject => $expect) {
$pattern = <<<REGEXP
/
(
(?:\p{Lu}\p{Ll}+) # Upper followed by lower
|
(?:\p{Lu}+(?!\p{Ll})) # Upper not followed by lower
|
(?:\p{N}+) # Runs of numbers
)
/ux
REGEXP;
$result = implode(
$separator,
preg_split(
$pattern,
str_replace($separator, '', $subject),
-1,
PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY,
),
);
echo
$result === $expect ? '✅' : '❌',
' ',
$result,
PHP_EOL;
}
- Output for 8.1.0 - 8.1.31, 8.2.0 - 8.2.26, 8.3.0 - 8.3.14, 8.4.1
- ✅ Some-Camel-Case
✅ Some-12-With-5-Numbers
✅ Some-PDF-In-Text
✅ 123-Leading-Numbers
✅ It-Is-2016
✅ What-If
✅ A-Single-Letter-B
✅ some_snake_case
✅ Title-_-Snake-_-Case
✅ lowerwithdash
✅ FFS-!
✅ With-A-😃
✅ PDF-123
✅ Emoji-In-The-🤞-Middle
✅ 12345
✅ 123-A
✅ A-123
✅ War-&-Peace
✅ lower-Then-Title-Case
✅ 123-lower
✅ lower-123
✅ It-Is-Über
✅ Søme-Thing
preferences:
58.78 ms | 407 KiB | 5 Q