Kishor
Kishor

Reputation: 1513

php explode for a special case

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

Upvotes: 0

Views: 101

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798676

$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"
}

Upvotes: 3

Related Questions