質問

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.

役に立ちましたか?

解決

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.

他のヒント

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top