I need to split on capital letters and numbers, with a series of capital letters getting split as an entire word anywhere in the string. It's probably easiest to just explain with examples, see the test script below:
$tests = array('FooBar', 'fooBar', 'Foobar', 'FooBar1', 'FooBAR', 'FooBARBaz', '1fooBar', '1FooBar', 'FOOBar');
foreach($tests as $test){ echo $test . " => " . split_camel_case($test) . "<br />"; }
function split_camel_case($root){
return implode(' ', preg_split('/(?<=[a-z])(?![a-z])/', $root, -1, PREG_SPLIT_NO_EMPTY));
}
What I get from that is:
FooBar => Foo Bar
fooBar => foo Bar
Foobar => Foobar
FooBar1 => Foo Bar 1
FooBAR => Foo BAR
FooBARBaz => Foo BARBaz
1fooBar => 1foo Bar
1FooBar => 1Foo Bar
FOOBar => FOOBar
The last four aren't correct, what they should be is:
FooBARBaz => Foo BAR Baz
1fooBar => 1 foo Bar
1FooBar => 1 Foo Bar
FOOBar => FOO Bar
I found the pattern in another StackOverflow question, but I haven't been able to find any that do exactly what I need. Any help would be greatly appreciated.
Just add the other cases as extra possiblities using
|
: