I'm running into an issue where I get a rather irregular string output from an RCON script on a server. When I send a command for returning players on a server, I get a string that looks like this:
rcon->get_players();
Players on server: [#] [IP Address]:[Port] [Ping] [GUID] [Name] -------------------------------------------------- 0 1.1.1.1:2 46 654321(OK) Player Name1 1 2.2.2.2:2 47 123456(OK) Player Name2 (2 players in total)
Here's what it looks like formatted if that helps:
Players on server:
[#] [IP Address]:[Port] [Ping] [GUID] [Name]
--------------------------------------------------
0 1.1.1.1:2 46 654321(OK) Player Name1
1 2.2.2.2:2 47 123456(OK) Player Name2
(2 players in total)
So the first 0 is their key id on the server (0-however many players), the second is their IP and PORT, the 47 is the ping, playerguid is their battleye guid, then their name inthe game, and then a total of the players returned.
However, it returns as one big string. I'm trying to figure out how to feed this into an array. So I get something like this:
array("id"=>"0", "Connection"=>"1.1.1.1:2", "ping"=>"46", "guid"=>"654321", "name"=>"Player Name1");
Any way I can achieve this, considering how irregular the output is? Having the headers in the string is throwing me off.
I followed Don't Panic's advice and it's close: UPDATED
echo "Player List:<br />";
$raw_players = $rcon->get_players();
$lines = explode("
", $raw_players);
$end = count($lines)-1;
$keys = array('id','connection','ping','guid','name');
$regex = '/(\d+)\s+([\d\.\:]+)\s+(\d+)\s+(\d+)\(OK\)\s+(.+)/';
for ($i=3; $i < $end; $i++) {
echo($lines[$i]);
preg_match($regex, $lines[$i], $matches);
unset($matches[0]);
echo(var_dump($matches));
$players[] = array_combine($keys, $matches);
}
And I get:
Player List:
0 98.193.210.251:2304 47 e0b29e3c7122bda33b5391c22594c776(OK) Colin Fox
array (size=0)
empty