18

I'm using this function to convert CamelCase to dashed string:

function camel2dashed($className) {
    return strtolower(preg_replace('/([^A-Z-])([A-Z])/', '$1-$2', $className));
}

it kinda works but theres problem when I have for ex. this string: getADog. It returns get-adog but I want get-a-dog

how should I change my code? Thanks

simPod
  • 11,498
  • 17
  • 86
  • 139

3 Answers3

38

Use a lookahead assertion:

function camel2dashed($className) {
    return strtolower(preg_replace('/([a-zA-Z])(?=[A-Z])/', '$1-', $className));
}

See it working online: ideone

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
8

You don't need a lookahead assertion to do this if you know that your string doesn't start with an upper-case letter, you can just insert a hyphen before every upper-case letter like this:

function camel2dashed($className) {
    return strtolower(preg_replace('/([A-Z])/', '-$1', $className));
}

This still won't handle cases like @sfjedi's "companyHQ" -> "company-hq". For that you'd have to explicitly test for permitted capitalized substrings that shouldn't be split, or specify some generic rules (e.g. don't prepend hyphen before last character).

You can find some more sophisticated alternatives in the answers to this virtual duplicate question.

Community
  • 1
  • 1
Ergwun
  • 12,579
  • 7
  • 56
  • 83
0
function camel2dash($name) {
   return ltrim(strtolower(preg_replace('/[A-Z]([A-Z](?![a-z]))*/', '-$0', $name)), '-');
}

Handles hopefully all edge cases:

SomeOfTheWords → some-of-the-words
getADog → get-a-dog
ADog → a-dog
aDog → a-dog
XML → xml
SimpleXMLReader → simple-xml-reader
Riki137
  • 2,076
  • 2
  • 23
  • 26