php explode at capital letters?

Look up preg_split

$result = preg_replace("([A-Z])", " $0", "helloMister");
print_r(explode(' ', $result));

hacky hack. Just don't have spaces in your input string.


If you want to split helloMister into hello and Mister you can use preg_split to split the string at a point just before the uppercase letter by using positive lookahead assertion:

$pieces = preg_split('/(?=[A-Z])/',$str);

and if you want to split it as hello and ister you can do:

$pieces = preg_split('/[A-Z]/',$str);

Tags:

Php

Function