-1

I have a string $text_arr="101104105106109111112113114116117120122123124" fairly big string

If i want to split three numbers from them like 101,104,105 and store them in $array .What should i do?

I tried doing this:

preg_match_all('/[0-9]{3}$/',"$text_arr",$array); 
000
  • 26,951
  • 10
  • 71
  • 101
Aditya
  • 4,453
  • 3
  • 28
  • 39

4 Answers4

5

The easiest way to do this is with preg_split()Docs:

$result = preg_split('/(\d{3})/', $str, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);

See it working, or the result:

Array
(
    [0] => 101
    [1] => 104
    [2] => 105
    [3] => 106
    [4] => 109
    [5] => 111
    [6] => 112
    [7] => 113
    [8] => 114
    [9] => 116
    [10] => 117
    [11] => 120
    [12] => 122
    [13] => 123
    [14] => 124
)
hakre
  • 193,403
  • 52
  • 435
  • 836
DaveRandom
  • 87,921
  • 11
  • 154
  • 174
  • Thanks a lot!! I have a similar array that i have to split based upon capital like "JimCaseyAlexShawn" ?? – Aditya Dec 31 '12 at 11:40
  • @Aditya Use the exact same line as above, but change the regex to `/([A-Z][a-z]+)/` – DaveRandom Dec 31 '12 at 11:42
  • @EliasVanOotegem For the given task, that is indeed true (and I should have thought of that). However, for the "split based on a capital" secondary question (see second comment on this answer) a regex is required - and that kind of validates this approach. You are are fundamentally correct though and you have a +1 from me. – DaveRandom Dec 31 '12 at 12:00
  • @DaveRandom: cheers, and I really should pay more attention to follow-up questions in comments before I go and leave comments like the one above, that make me sound like a bit of a pedantic, know-it-all, twat :P +1 right back at you :) – Elias Van Ootegem Dec 31 '12 at 12:06
2

Though you could use a regular expression for this, it might be more performant to use a simple, standard function:

$groups = str_split($numbers, 3);//returns array you want

Read all about it here

Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
  • I am getting the result like this: Array ( [0] => [1] => 101 ) Array ( [0] => ) Array ( [0] => 104 ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => 105 ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => 106 ) Array ( [0] => 109 ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => 111 ) Array ( [0] => ) Array ( [0] => 112 ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => 113 ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => 114 ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => 116 ) Array ( [0] => 117 ) – Aditya Dec 31 '12 at 12:11
  • @Aditya: I'm not, `str_split('123456789123456789',3);` works just fine. Check your input string, or set-up a pastebin or codepad, so we can see what you're doing – Elias Van Ootegem Dec 31 '12 at 12:16
  • @Aditya: you're welcome, but would you mind awfully accepting DaveRandom's answer? his answer (in combination with his comments) are the most complete answer here, and he did make an effort :) – Elias Van Ootegem Dec 31 '12 at 13:27
1

You have to remove the ends with $ from your expression, it is causing to return only one result

try like this

preg_match_all('/[0-9]{3}/', $text_arr, $array); 

check this working here

Pankaj Khairnar
  • 3,028
  • 3
  • 25
  • 34
1

Choose this simplest code

<?php
    $string = "101104105106109111112113114116117120122123124";
    $parts = str_split($string, 3);
    $res=implode(',',$parts);
    echo($res);
?>
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
prasobh
  • 95
  • 3