fork(9) download
  1. <?php
  2.  
  3. function camelToUS($string, $us = "-") {
  4. '/(?<=\d)(?=[A-Za-z])|(?<=[A-Za-z])(?=\d)|(?<=[a-z])(?=[A-Z])/', $us, $string));
  5. }
  6.  
  7. $test_values = [
  8. 'foo' => 'foo',
  9. 'fooBar' => 'foo-bar',
  10. 'foo123' => 'foo-123',
  11. '123Foo' => '123-foo',
  12. 'fooBar123' => 'foo-bar-123',
  13. 'foo123Bar' => 'foo-123-bar',
  14. '123FooBar' => '123-foo-bar',
  15. ];
  16.  
  17. foreach ( $test_values as $key => $val ) {
  18. echo "$key: " . camelToUS($key) . " => " . (camelToUS($key) == $val) . "\n";
  19. }
  20.  
  21. ?>
Success #stdin #stdout 0.01s 52488KB
stdin
Standard input is empty
stdout
foo: foo => 1
fooBar: foo-bar => 1
foo123: foo-123 => 1
123Foo: 123-foo => 1
fooBar123: foo-bar-123 => 1
foo123Bar: foo-123-bar => 1
123FooBar: 123-foo-bar => 1