Nov 11 2010

Splitting string at capitalized letters

Category: Articles,Code snippets,PHP,PHPFractalizeR @ 7:42 pm

Recently I had a task to split a given string at capitalized characters, having those saved in PHP. I’ve invented the following solution:

$str = 'TestMyFuncCall';
var_dump(preg_split('/(?=[A-Z])/', $str, null, PREG_SPLIT_NO_EMPTY));

The result is:

array(4) {
  string(4) "Test"
  string(2) "My"
  string(4) "Func"
  string(4) "Call"

I utilize regular expressions assertions here to avoid character loss.

Leave a Reply

You must be logged in to post a comment. Login now.