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
It will be slow as I have 1000+ packagesand this is concluded, how? I've done a little bench on the two answers and concluded that looping though is around62.68%faster than regex.