Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I'm newbie php programmer.

i serious problem T_T....

use php preg_match_all with regex,

$pattern = "/<<>>(.*){0,20}?(<<>>)/";
$text = <<>>id1<<>>2<<>>3<<>>4<<>>5<<>>6<<>>7<<>>8<>>9<<>>10<<>>11<<>>12<<>>13<<>>14<<>>15<<>>16<<>>17<<>>18<<>>19<<>>20<<>>21<<>>";
$text.= "<<>>id2<<>>2<<>>3<<>>4<<>>5<<>>6<<>>7<<>>8<>>9<<>>10<<>>11<<>>12<<>>13<<>>14<<>>15<<>>16<<>>17<<>>18<<>>19<<>>20<<>>21<<>>";
$text.= "<<>>id3<<>>2<<>>3<<>>4<<>>5<<>>6<<>>7<<>>8<>>9<<>>10<<>>11<<>>12<<>>13<<>>14<<>>15<<>>16<<>>17<<>>18<<>>19<<>>20<<>>21<<>>";

preg_match_all($pattern, $text, $match);

this result.

Array (
[0] => Array ([0] => <<>>id1<<>>2<<>>3<<>>4<<>>5<<>>6<<>>7<<>>8<>>9<<>>10<<>>11<<>>12<<>>13<<>>14<<>>15<<>>16<<>>17<<>>18<<>>19<<>>20<<>>21<<>><<>>id2<<>>2<<>>3<<>>4<<>>5<<>>6<<>>7<<>>8<>>9<<>>10<<>>11<<>>12<<>>13<<>>14<<>>15<<>>16<<>>17<<>>18<<>>19<<>>20<<>>21<<>><<>>id3<<>>2<<>>3<<>>4<<>>5<<>>6<<>>7<<>>8<>>9<<>>10<<>>11<<>>12<<>>13<<>>14<<>>15<<>>16<<>>17<<>>18<<>>19<<>>20<<>>21<<>> )
[1] => Array ( [0] => id1<<>>2<<>>3<<>>4<<>>5<<>>6<<>>7<<>>8<>>9<<>>10<<>>11<<>>12<<>>13<<>>14<<>>15<<>>16<<>>17<<>>18<<>>19<<>>20<<>>21<<>><<>>id2<<>>2<<>>3<<>>4<<>>5<<>>6<<>>7<<>>8<>>9<<>>10<<>>11<<>>12<<>>13<<>>14<<>>15<<>>16<<>>17<<>>18<<>>19<<>>20<<>>21<<>><<>>id3<<>>2<<>>3<<>>4<<>>5<<>>6<<>>7<<>>8<>>9<<>>10<<>>11<<>>12<<>>13<<>>14<<>>15<<>>16<<>>17<<>>18<<>>19<<>>20<<>>21 )
[2] => Array ( [0] => <<>> )
)

however, i want result, below.

[0] => (

[0] => id1
[1] => 1
[2] => 2
...
)

[1] => (

[0] => id2
[1] => 1
[2] => 2
...
)

how do regex pattern, this result?

Edit1.

array_filter(preg_split('/>/', $text));

Array ( [1] => id1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 9 [10] => 10 [11] => 11 [12] => 12 [13] => 13 [14] => 14 [15] => 15 [16] => 16 [17] => 17 [18] => 18 [19] => 19 [20] => 20 [21] => 21 [23] => id2 [24] => 2 [25] => 3 [26] => 4 [27] => 5 [28] => 6 [29] => 7 [30] => 8 [31] => 9 [32] => 10 [33] => 11 [34] => 12 [35] => 13 [36] => 14 [37] => 15 [38] => 16 [39] => 17 [40] => 18 [41] => 19 [42] => 20 [43] => 21 [45] => id3 [46] => 2 [47] => 3 [48] => 4 [49] => 5 [50] => 6 [51] => 7 [52] => 8 [53] => 9 [54] => 10 [55] => 11 [56] => 12 [57] => 13 [58] => 14 [59] => 15 [60] => 16 [61] => 17 [62] => 18 [63] => 19 [64] => 20 [65] => 21 )

but, i want

[0] => ([0] =>id1 [1]=> 1 ...)
[1] => ([0] =>id2 [1]=> 1 ...)
[2] => ([0] =>id3 [1]=> 1 ...)

how do this?

share|improve this question
up vote 0 down vote accepted

Alternative approach: exploding your string

Instead of using regex (which is slow), try splitting your line using explode() function.

explode('<<>>', $text);

This will create empty elements at the beginning and at the end of the array. To get rid of them, you can use array_filter:

array_filter(explode('<<>>', $text));

I noticed, however, that between 8 and 9 you have <>> (note missing <). If this is desired and you need to deal with that kind of input too, you can use preg_split, which will allow you to do the same thing as explode, but with "multiple" delimiters:

array_filter(preg_split('/<?<>>/', $text));

Original approach: preg_match_all

If you, however, insist on using preg_match_all, then you need to be aware of a few things:

  1. {0,20} makes * unnecessary. I.e. what you're looking after is .{0,20}, not .*{0,20}. Otherwise it makes no sense.
  2. You can prevent <<>> from appearing in the result by surrounding them with non-capturing group like this: (?:<<>>).
  3. To take care of excessive greediness, you can use ? operator after operators like {x,y}, * and +. This will make matching lazy.

Having this in mind, we can construct the following regex:

preg_match_all('/(.{0,20}?)(?:<?<>>)/', $text, $matches);

This will create one empty element at the beginning of the array. I leave it up to you to get rid of it.


As you see, using preg_match_all is a lot more complicated than using explode() or preg_split. That's why I recommend you to use the first approach.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.