3v4l.org

run code in 300+ PHP versions simultaneously
<?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; }

preferences:
35.51 ms | 406 KiB | 5 Q