Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i take only some specific data by explode?

Tags:

php

explode

In my following code I can explode and get my 3 elements imei,lat,lon as the response was returning only the 3 data separated by ' '.

   $r = socket_recvfrom($sock, $buf, 512, 0, $remote_ip, $remote_port);
    echo "$remote_ip : $remote_port -- " . $buf;

    list($imei,$lat,$lon) = explode(' ', $buf, 3); 

But what if the response is sending me a lot of data separated by ' ' but still i need only the 3 elements. I don't need the rest data. How can i explode like that?

The data will not be the first three items returned by explode().

like image 410
hhs Avatar asked Oct 20 '22 02:10

hhs


1 Answers

Just add one more comma to list() to ignore the remaining values:

list($imei,$lat,$lon,) = explode(' ', $buf); 

Demo

Or, if the values are in specific places in your array:

list($imei, , $lat, , $lon,) = explode(' ', $buf); 

Demo

The empty placeholders "skip" those values and they are never assigned to a variable.

like image 107
John Conde Avatar answered Oct 29 '22 03:10

John Conde