fork 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. '123FOOBar' => '123-foo-bar',
  16. '123FooBAR' => '123-foo-bar',
  17. 'FooBAR123' => 'foo-bar-123',
  18. 'FOOBar123' => 'foo-bar-123',
  19. ];
  20.  
  21. foreach ( $test_values as $key => $val ) {
  22. echo "$key: " . camelToUS($key) . " => " . (camelToUS($key) == $val) . "\n";
  23. }
  24.  
  25. ?>
Success #stdin #stdout 0.03s 26080KB
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
123FOOBar: 123-foobar => 
123FooBAR: 123-foo-bar => 1
FooBAR123: foo-bar-123 => 1
FOOBar123: foobar-123 =>