1

Before: ThisIsExample After: This-Is-Example

It's possible do with regular expression ? I try do this by exploding by upper case letter but it's impossible devide string by upper case leteer.

Wizard
  • 10,985
  • 38
  • 91
  • 165

2 Answers2

4

You can do it like this:

$result = preg_replace('~[a-z]\K(?=[A-Z])~', '-', $yourString);

\K reset all that have been matched before, then you can with this trick match all uppercase preceded by a lowercase.

(?=..) is a lookahead and means followed by. A lookahead is just a check but match nothing.

Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
2

Yes, it's possible using a regular expression. Please see this StackOverflow answer: https://stackoverflow.com/a/6227110
The answer provides a solution to add a underscore before a capital letter, so to add a dash (the - sign), this is the correct code:

$result = preg_replace('/\B([A-Z])/', '-$1', $subject);

Hope this helps.

Community
  • 1
  • 1
ProgramFOX
  • 6,131
  • 11
  • 45
  • 51