0

I need to split multiple data packets to single chunk

$packets=" *TS01,868323025167807,205004181116,GPS:3;S33.792541;E151.066264;58;347;1.46,STT:202;0,MGR:1555391,ADC:0;13.84;1;38.89;2;4.14,EVT:1#*TS01,868323025167807,205104181116,GPS:3;S33.786623;E151.060349;66;309;1.10,STT:202;0,MGR:1556426,ADC:0;13.78;1;38.70;2;4.14,EVT:1#*TS01,868323025167807,205204181116,GPS:3;S33.779947;E151.052654;47;313;1.10,STT:202;0,MGR:1557577,ADC:0;13.65;1;38.70;2;4.14,EVT:1#*TS01,868323025167807,205304181116,GPS:3;S33.776746;E151.051546;52;346;1.09,STT:202;0,MGR:1557980,ADC:0;13.85;1;38.14;2;4.14,EVT:1#*TS01,868323025167807,205404181116,GPS:3;S33.768219;E151.048295;63;338;1.08,STT:202;0,MGR:1559134,ADC:0;13.62;1;37.95;2;4.14,EVT:1#*TS01,868323025167807,205504181116,GPS:3;S33.763735;E151.047204;0;0;1.08,STT:202;0,MGR:1559699,ADC:0;12.84;1;37.95;2;4.14,EVT:1#*TS01,868323025167807,205518181116,GPS:3;S33.763707;E151.047218;0;0;1.07,STT:2;0,MGR:1559699,ADC:0;12.84;1;37.95;2;4.14,EVT:F0;200#"; 

One valid packet looks like below

*TS01,868323025167807,205004181116,GPS:3;S33.792541;E151.066264;58;347;1.46,STT:202;0,MGR:1555391,ADC:0;13.84;1;38.89;2;4.14,EVT:1# 

Means every packet start with * and end with #

I did the following

$no_of_packets=substr_count($packets, '*'); if($no_of_packets>1){ //explode with # $packets=explode("#",$msg) } 

I got array like below

[0] => *TS01,868323025167807,205004181116,GPS:3;S33.792541;E151.066264;58;347;1.46,STT:202;0,MGR:1555391,ADC:0;13.84;1;38.89;2;4.14,EVT:1 [1] => *TS01,868323025167807,205104181116,GPS:3;S33.786623;E151.060349;66;309;1.10,STT:202;0,MGR:1556426,ADC:0;13.78;1;38.70;2;4.14,EVT:1 [2] => *TS01,868323025167807,205204181116,GPS:3;S33.779947;E151.052654;47;313;1.10,STT:202;0,MGR:1557577,ADC:0;13.65;1;38.70;2;4.14,EVT:1 [3] => *TS01,868323025167807,205304181116,GPS:3;S33.776746;E151.051546;52;346;1.09,STT:202;0,MGR:1557980,ADC:0;13.85;1;38.14;2;4.14,EVT:1 [4] => *TS01,868323025167807,205404181116,GPS:3;S33.768219;E151.048295;63;338;1.08,STT:202;0,MGR:1559134,ADC:0;13.62;1;37.95;2;4.14,EVT:1 [5] => *TS01,868323025167807,205504181116,GPS:3;S33.763735;E151.047204;0;0;1.08,STT:202;0,MGR:1559699,ADC:0;12.84;1;37.95;2;4.14,EVT:1 [6] => *TS01,868323025167807,205518181116,GPS:3;S33.763707;E151.047218;0;0;1.07,STT:2;0,MGR:1559699,ADC:0;12.84;1;37.95;2;4.14,EVT:F0;200 

The '#' is missing . If i go thru the loop and append '#' to each packets it will be too slow as I have 1000+ packets sometime

I need to preserve '#' and split by '#'

I tried the solution from Is there way to keep delimiter while using php explode or other similar functions? but it gave last empty array too

2

2 Answers 2

2

The best is to set empty separator and use positive look behind / look ahead for # character and any character respectively:

$list = preg_split('/(?<=#)(?=.)/', $msg); 

(?<=#) is positive look behind for # character, (?=.) is positive look ahead for any character (this avoids generating empty string at the end).

Actually, if * is part of separator, the following regexp would work even better (more reliably):

$list = preg_split('/(?<=#)(?=\*)/', $msg); 
Sign up to request clarification or add additional context in comments.

1 Comment

(?=.) is awesome
0

This code will help you for getting you your desired output -

$arr = explode("*", $packets); array_shift($arr); $result = array(); for($i = 0; $i<count($arr); $i++){ array_push($result, "*".$arr[$i]); } print_r($result); 

1 Comment

I am not sure why this has been down-voted, it works and it should be faster in execution than the excepted answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.