2

Using the following code, php explode at capital letters?

you can explode the string by uppercase. But how do you explode it only on the first uppercase? Say you have helloThereMister. I want to get hello ThereMister. I can concatenate the result from the link above, but if there is a way to skip it, then fantastic!

Thanks

Community
  • 1
  • 1
Kousha
  • 32,871
  • 51
  • 172
  • 296
  • 3
    Using the `limit` argument for [preg_split()](http://www.php.net/manual/en/function.preg-split.php) – Mark Baker Feb 17 '14 at 22:51
  • `preg_split("[A-Z]{1}", $str);`? – putvande Feb 17 '14 at 22:51
  • @putvande This will still match every capital. `{1}` doesn't limit, it just states (redundantly) that you want to find each singular capitalized alphabetical letter. –  Feb 17 '14 at 22:53
  • 1
    Like @MarkBaker says: `preg_split("/(?=\p{Lu})/","helloThereMister",2)` – Wrikken Feb 17 '14 at 22:56

2 Answers2

4

RTM my friend, as per documentation of preg_split you have also a $limit parameter so the answer is:

$pieces = preg_split('/(?=[A-Z])/', $str, 1);
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
pna
  • 5,651
  • 3
  • 22
  • 37
  • 1
    `(?=...)` is not needed in this case ;). It also causes great slowdowns on bigger RegEx, so please avoid it if not needed :). Furthermore +1 for the correct answer. –  Feb 17 '14 at 22:55
  • @pna: I'm sorry for you, but `(?=[A-Z])` was a correct pattern. Now it is false. – Casimir et Hippolyte Feb 18 '14 at 00:13
1

look at manual for preg_split, third argument

Michael Livach
  • 518
  • 3
  • 13