Reputation: 28040
Let say I have the following string:
getPasswordLastChangedDatetime
How would I be able to split that up by capital letters so that I would be able to get:
get
Password
Last
Changed
Datetime
Upvotes: 4
Views: 2982
Reputation: 816580
If you only care about ASCII characters:
$parts = preg_split("/(?=[A-Z])/", $str);
The (?= ..)
construct is called lookahead [docs].
This works if the parts only contain a capital character at the beginning. It gets more complicated if you have things like getHTMLString
. This could be matched by:
$parts = preg_split("/((?<=[a-z])(?=[A-Z])|(?=[A-Z][a-z]))/", $str);
Upvotes: 7
Reputation: 28040
Asked this a little too soon, found this:
preg_replace('/(?!^)[[:upper:]]/',' \0',$test);
Upvotes: 1
Reputation: 48121
No need to over complicated solution. This does it
preg_replace('/([A-Z])/',"\n".'$1',$string);
This doens't take care of acronyms of course
Upvotes: 0