0

I have a huge text file which I want to explode into an array.

The words in it doesnt have spaces, but each word starts with a capital letter.

How can I explode it to an array taking the capital letters as seperator,without losing the charector?

AppleBallCat should be 1 => Apple 2=> Ball 3=> Cat

mechanical_meat
  • 163,903
  • 24
  • 228
  • 223
Kishor
  • 1,513
  • 2
  • 15
  • 25
  • 1
    Seems like a duplicate of http://stackoverflow.com/questions/6920155/how-does-one-break-a-string-down-by-capital-letters-with-php – kijin Mar 30 '12 at 01:45
  • Sorry! couldnt find that before. – Kishor Mar 30 '12 at 01:49
  • possible duplicate of [PHP explode the string, but treat words in quotes as a single word](http://stackoverflow.com/questions/2202435/php-explode-the-string-but-treat-words-in-quotes-as-a-single-word) –  Mar 30 '12 at 02:20

1 Answers1

3
$s = 'AppleBallCat';
$a = preg_split('/(?=[A-Z])/', $s);
unset($a[0]);
var_dump($a);
array(3) {
  [1]=>
  string(5) "Apple"
  [2]=>
  string(4) "Ball"
  [3]=>
  string(3) "Cat"
}
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358