Frage

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.

War es hilfreich?

Lösung

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.

Andere Tipps

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top