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
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);
Asked this a little too soon, found this:
preg_replace('/(?!^)[[:upper:]]/',' \0',$test);
For instance:
(?:^|\p{Lu})\P{Lu}*
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
preg_split('@(?=[A-Z])@', 'asAs')