Trying to make string output an array in php -


i'm running issue rather irregular string output rcon script on server. when send command returning players on server, string looks 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 looks formatted if 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 first 0 key id on server (0-however many players), second ip , port, 47 ping, playerguid battleye guid, name inthe game, , total of players returned.

however, returns 1 big string. i'm trying figure out how feed array. this:

array("id"=>"0", "connection"=>"1.1.1.1:2", "ping"=>"46", "guid"=>"654321", "name"=>"player name1"); 

any way can achieve this, considering how irregular output is? having headers in string throwing me off.

i followed don't panic's advice , it's close: updated

echo "player list:<br />"; $raw_players = $rcon->get_players(); $lines = explode("\n", $raw_players); $end = count($lines)-1; $keys = array('id','connection','ping','guid','name'); $regex = '/(\d+)\s+([\d\.\:]+)\s+(\d+)\s+(\d+)\(ok\)\s+(.+)/'; ($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 get:

player list: 0 98.193.210.251:2304 47 e0b29e3c7122bda33b5391c22594c776(ok) colin fox array (size=0)   empty 

first, explode based on newlines array of each line.

$lines = explode("\n", $string); 

then can construct loop excluding header , footer this:

$end = count($lines) - 1; // exclude last line ($i=3; $i < $end; $i++) { // start @ fourth line 

inside loop, can use explode again, space delimiter, array every line.

$players[] = explode(' ', $lines[$i], 5); 

this should work, because looks values @ beginning of each line not have spaces. third argument (5) prevent player name being split on space (if contains space) because restricts size of array generated explode 5 elements.

if want resulting array have string keys, can define array of keys (before loop):

$keys = array('id', 'connection', 'ping', 'guid', 'name'); 

and use array_combine in loop create each player array.

$players[] = array_combine($keys, explode(' ', $lines[$i], 5)); 

with new info string, seems of columns separated more 1 space (and not same number of spaces), explode not work this. can use regular expression match in loop instead.

$keys = array('id', 'connection', 'ping', 'guid', 'name'); $regex = '/(\d+)\s+([\d\.\:]+)\s+(\d+)\s+(\w+)\(ok\)\s+(.+)/'; ($i=3; $i < $end; $i++) {     preg_match($regex, $lines[$i], $matches);     unset($matches[0]);     $players[] = array_combine($keys, $matches); } 

the unset($matches[0]); because first element in preg_match matches entire string. each of subsequent values in $matches contents of capture groups. here explanation of regex:

/            begin pattern (\d+)        capture 1 or more digits (id) \s+          skip 1 or more spaces ([\d\.\:]+)  capture 1 or more characters either digit, dot, or colon (ip/port) \s+          skip 1 or more spaces (\d+)        capture 1 or more digits (ping) \s+          skip 1 or more spaces (\w+)        capture 1 or more alphanumeric characters (guid) \(ok\)\s+    skip (ok) , 1 or more spaces (.+)         capture else (player name) /            end pattern 

Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -