所以我有一些来自查询游戏服务器的数据,我需要找到一种方法来提取信息,以便它可以在我的网站上使用。
# 7 "player1" 1156 STEAM_0:4:00100365 0 09:52 84 3 175.77.234.241:27505
# 8 "player2" 1149 STEAM_0:0:39163243 2 14:05 52 0 109.175.131.239:65423
# 9 "player " 1159 STEAM_0:0:162 31 07:19 45 0 78.1.126.130:27025
#10 "gameer123" 1146 STEAM_0:0:701135 4 16:41 94 0 191.200.0.100:14097
上面的行格式如下: " ID" "名称" "用户ID" " UNIQUEID" "断枝" "时间" "平" "损失" " ADR"
如何提取此信息并将其转换为数组,以便我有类似的内容
[0] => Array
(
[Id] => 0
[Name] => Player0
[Frags] => 0
[Time] => 50856
[Uniqueid] => STEAM_0:4:001534362
)
[1] => Array
(
[Id] => 0
[Name] => Player1
[Frags] => 4
[Time] => 1447
[Uniqueid] => STEAM_0:4:00100365
)
顺便说一下,我只需要玩家名字,frags,时间和uniqueid。
答案 0 :(得分:2)
$keys = ["id", "name", "userid", "uniqueid", "frag", "time", "ping", "loss", "adr"];
$result = [];
foreach($array as $row){
$row_array = preg_split('/\s+\"([^\"]+)\"\s+|#\s*|\s+/', $row, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
$result[] = array_combine($keys, $row_array);
}
var_export($result);
结果
array (
0 =>
array (
'id' => '7',
'name' => 'player1',
'userid' => '1156',
'uniqueid' => 'STEAM_0:4:00100365',
'frag' => '0',
'time' => '09:52',
'ping' => '84',
'loss' => '3',
'adr' => '175.77.234.241:27505',
),
1 =>
array (
'id' => '8',
'name' => 'player2',
'userid' => '1149',
'uniqueid' => 'STEAM_0:0:39163243',
'frag' => '2',
'time' => '14:05',
'ping' => '52',
'loss' => '0',
'adr' => '109.175.131.239:65423',
),
2 =>
array (
'id' => '9',
'name' => 'player ',
'userid' => '1159',
'uniqueid' => 'STEAM_0:0:162',
'frag' => '31',
'time' => '07:19',
'ping' => '45',
'loss' => '0',
'adr' => '78.1.126.130:27025',
),
3 =>
array (
'id' => '10',
'name' => 'gameer123',
'userid' => '1146',
'uniqueid' => 'STEAM_0:0:701135',
'frag' => '4',
'time' => '16:41',
'ping' => '94',
'loss' => '0',
'adr' => '191.200.0.100:14097',
),
)
答案 1 :(得分:2)
这是另一种RegEx方法,使用直接回收到数组键中的命名模式,用于干净的最终数组。您可以使用下面的解决方案使用此处建议的任何RegEx模式集,只需在每个模式的开头添加(?<Keyname>...)
即可。用括号命名。
$data = <<<EOL
# 7 "player1" 1156 STEAM_0:4:00100365 0 09:52 84 3 175.77.234.241:27505
# 8 "player2" 1149 STEAM_0:0:39163243 2 14:05 52 0 109.175.131.239:65423
# 9 "player" 1159 STEAM_0:0:162 31 07:19 45 0 78.1.126.130:27025
#10 "gameer123" 1146 STEAM_0:0:701135 4 16:41 94 0 191.200.0.100:14097
EOL;
/* Matching the source data with named patterns */
preg_match_all('#(?<Id>\d+)\s+"(?<Name>[[:alnum:]]+)"\s+\d+\s+(?<Uniqueid>STEAM_[0-9_:]+)\s+(?<Frags>\d+)\s+(?<Time>[\d:]{5})#m', $data, $fields, PREG_SET_ORDER);
/* Cleaning up the numeric keys from RegEx matches */
foreach($fields as $num=>$set) {
$fields[$num] = array_diff_key($set, array_flip( array_filter( array_keys($set), 'is_int')) );
}
print_r($fields);
结果:
Array
(
[0] => Array
(
[Id] => 7
[Name] => player1
[Uniqueid] => STEAM_0:4:00100365
[Frags] => 0
[Time] => 09:52
)
[1] => Array
(
[Id] => 8
[Name] => player2
[Uniqueid] => STEAM_0:0:39163243
[Frags] => 2
[Time] => 14:05
)
[2] => Array
(
[Id] => 9
[Name] => player
[Uniqueid] => STEAM_0:0:162
[Frags] => 31
[Time] => 07:19
)
[3] => Array
(
[Id] => 10
[Name] => gameer123
[Uniqueid] => STEAM_0:0:701135
[Frags] => 4
[Time] => 16:41
)
)
请注意,我已将源数据的第三行从"player "
修剪为"player"
,假设这是一个错字。如果玩家姓名包含其他字符,请将其添加到:(?<Name>[[:alnum:] _-]+)
等。
======
我将命名模式的int-key-cleaner回收到一个函数中,以防有人需要经常使用。
/* In place of the foreach-loop above, use: */
$fields = remove_int_keys_pma($fields);
/* And stick these two functions somewhere. */
/* Remove integer keys from preg_match_all sets. (Must use PREG_SET_ORDER.) */
function remove_int_keys_pma($array) {
foreach($array as $k=>$set) {
$array[$k] = remove_int_keys($set);
}
return $array;
}
/* Remove integer keys from an array. */
function remove_int_keys($array) {
return array_diff_key($array, array_flip( array_filter( array_keys($array), 'is_int')) );
}
答案 2 :(得分:1)
您可以使用函数explode();
$result = [];
$number = 0;
foreach($mysql_result_rows as $row){
$row_array = explode(' ', $row);
$result[$number]['Id'] = trim($row_array['0'], ' ');
$result[$number]['Name'] = trim($row_array['1'], '"');
$result[$number]['Frags'] = trim($row_array['4'], ' ');
$result[$number]['Time'] = trim($row_array['5'], ' ');
$result[$number]['Uniqueid'] = trim($row_array['3'], ' ');
$number++;
}
答案 3 :(得分:1)
我对您的数据做了一些假设,并且可能有一个更简化的正则表达式来完成此任务,但无论如何这是一个开始。
以下代码:
$pattern = '/#\s*(\d+)\s+"([a-zA-Z0-9\s]+)"\s+(\d+)\s+(STEAM_\d+:\d+:\d+)\s+(\d+)\s+(\d+:\d+)\s+(\d+)\s+(\d+)\s+(\d+.\d+.\d+.\d+:\d+)/';
$subject = '# 7 "player1" 1156 STEAM_0:4:00100365 0 09:52 84 3 175.77.234.241:27505
# 8 "player2" 1149 STEAM_0:0:39163243 2 14:05 52 0 109.175.131.239:65423
# 9 "player " 1159 STEAM_0:0:162 31 07:19 45 0 78.1.126.130:27025
#10 "gameer123" 1146 STEAM_0:0:701135 4 16:41 94 0 191.200.0.100:14097';
$lines = explode("\n",$subject);
$allMatches = array();
foreach($lines as $line) {
preg_match($pattern,$line,$matches); // Extract array of fields
$allMatches[] = array(
"Id" => $matches[1],
"Name" => $matches[2],
"Frags" => $matches[5],
"Time" => $matches[6],
"Uniqueid" => $matches[4]
);
}
var_export($allMatches);
给我这个结果:
array (
0 =>
array (
'Id' => '7',
'Name' => 'player1',
'Frags' => '0',
'Time' => '09:52',
'Uniqueid' => 'STEAM_0:4:00100365',
),
1 =>
array (
'Id' => '8',
'Name' => 'player2',
'Frags' => '2',
'Time' => '14:05',
'Uniqueid' => 'STEAM_0:0:39163243',
),
2 =>
array (
'Id' => '9',
'Name' => 'player ',
'Frags' => '31',
'Time' => '07:19',
'Uniqueid' => 'STEAM_0:0:162',
),
3 =>
array (
'Id' => '10',
'Name' => 'gameer123',
'Frags' => '4',
'Time' => '16:41',
'Uniqueid' => 'STEAM_0:0:701135',
),
)
正如Markus-ao所指出的,我在这里展示的正则表达式模式旨在解析字符串中的每个元素。要直接解决问题,以下模式更合适:
$pattern = '/#\s*(\d+)\s+"([a-zA-Z0-9\s]+)"\s+(?:\d+)\s+(STEAM_\d+:\d+:\d+)\s+(\d+)\s+(\d+:\d+)/';
请注意,此更改还会将匹配数组定义更改为:
$allMatches[] = array(
"Id" => $matches[1],
"Name" => $matches[2],
"Frags" => $matches[4],
"Time" => $matches[5],
"Uniqueid" => $matches[3]
);
答案 4 :(得分:0)
Yeap同样在这里:)
我已使用Regex 101网站微调模式,以尽可能准确地获取所需的细分。
其余的基本上是一种机制,可以通过它们来组装和打印结果。
<?php
$sampleGmeServerResponse = [
'# 7 "player1" 1156 STEAM_0:4:00100365 0 09:52 84 3 175.77.234.241:27505',
'# 8 "player2" 1149 STEAM_0:0:39163243 2 14:05 52 0 109.175.131.239:65423',
'# 9 "player " 1159 STEAM_0:0:162 31 07:19 45 0 78.1.126.130:27025',
'#10 "gameer123" 1146 STEAM_0:0:701135 4 16:41 94 0 191.200.0.100:14097',
];
$keys = ['id', 'name', 'userid', 'uniqueid', 'frag', 'time', 'ping', 'loss', 'adr'];
// The regex made here : https://regex101.com/r/fL9pD7/4 :p
$pattern = '/\s?+(\d+)\s+\"([\w\s]+)\"\s(\d+)\s([\w:"]+)\s+(\d+)\s(\d{2}:\d{2})\s+(\d+)\s+(\d+)\s([\d.:]+)/';
$result = [];
foreach ($sampleGmeServerResponse as $lineOfData) {
$matches = [];
preg_match($pattern, $lineOfData, $matches);
array_shift($matches);
$result[] = array_combine($keys, $matches);
}
print_r($result);
导致以下输出:
Array
(
[0] => Array
(
[id] => 7
[name] => player1
[userid] => 1156
[uniqueid] => STEAM_0:4:00100365
[frag] => 0
[time] => 09:52
[ping] => 84
[loss] => 3
[adr] => 175.77.234.241:27505
)
[1] => Array
(
[id] => 8
[name] => player2
[userid] => 1149
[uniqueid] => STEAM_0:0:39163243
[frag] => 2
[time] => 14:05
[ping] => 52
[loss] => 0
[adr] => 109.175.131.239:65423
)
[2] => Array
(
[id] => 9
[name] => player
[userid] => 1159
[uniqueid] => STEAM_0:0:162
[frag] => 31
[time] => 07:19
[ping] => 45
[loss] => 0
[adr] => 78.1.126.130:27025
)
[3] => Array
(
[id] => 10
[name] => gameer123
[userid] => 1146
[uniqueid] => STEAM_0:0:701135
[frag] => 4
[time] => 16:41
[ping] => 94
[loss] => 0
[adr] => 191.200.0.100:14097
)
)
答案 5 :(得分:0)
您可以执行此操作,但必须先从$ arr中删除不需要的数据。
$game_array = array();
$key = array("id", "name", "frags", "time", "uniqueid");
$str = '7 "player1" 1156 STEAM_0:4:00100365 0 09:52 84 3 175.77.234.241:27505';
$arr = explode(" ", $str);
$arr = array_filter($arr);
$first = array_combine($key, $arr);
$game_array[0] = $first;