PHP - 读取结果txt文件以获取变量

时间:2014-08-06 21:56:50

标签: php mysql text-files

我正在尝试做的事情看起来不可能用我的实际PHP技能。下面你可以在txt中找到一个比赛结果文件的例子。该文件由以下内容组成:

dir= the-track-file-name
longname= the-track-long-name-with-spaces
firstlap= the number of gate (checkpoint) the first lap is composed of
normallap= the number of gate (checkpoint) other laps are composed of
holeshotindex= thefirst gate after the start, which determine which player started first
time= the race duration, in minutes
laps= the number of laps (if minutes + laps, laps are counted when time's up)
starttime=1793
date= timestamp of the start
players:(under this line are all the player, just 1 in this exemple)
slot=0 (this is the multiplayer server slot taken by the player)
uid=5488 (this is the unique ID of the player)
number=755 (player's race number)
bike=rm125 (player's motorbike model)
name=Nico #755(player's name)
times: (under this line are things like timestamps of every gate, like SLOT|GATE|TIME)
0 0 1917 (it's like divide the timstamp /128 sounds good)
0 1 2184
(and etc, see full exemple below...)

游戏服务器位于专用的ubuntu上。 在每个比赛结束时,我将这些结果发送到FTP网络服务器上,我需要的是在选择比赛后(在下拉列表中)让vars输出可读表格的结果。 做表不是问题。 我的问题是,即使在这里搜索很多,我也不知道如何阅读txt来获取这种页面(仅限结果表):http://mxsimulator.com/servers/q2.MXSConcept.com/races/6015.html

以下是完整的示例结果文件:http://www.mediafire.com/view/3b34a4kd5nfsj4r/sample_result_file.txt

谢谢

1 个答案:

答案 0 :(得分:1)

好的,今晚它的文件解析时间。

我已经编写了一个非常基本的解析器,它逐行遍历数据。 首先,它寻找" ="。当" ="发现该行在" ="处被拆分/爆炸。 你得到两个部分:" ="之前和之后。 我已将它们用作$ results数组中的键和值。

这个过程一直持续到我们到达"时间:"。 这条线表示在下一行(行"时间:" + 1),结果开始。 结果是"槽门时间"由空格分隔。因此,结果以#34; " (空间)这次你得到了三个部分。 我已经插入了一个数组键' times'其中包含一个带有命名键的数组(槽,门,时间)。

你可能只看一下$ results数组的结构。 迭代它以呈现表格或输出数据应该非常容易。

#$datafile = 'http://www.mediafire.com/view/3b34a4kd5nfsj4r/sample_result_file.txt';
#$lines = file_get_contents($datafile);

$lines = '
dir=Dardon-Gueugnon
longname=Dardon Gueugnon
firstlap=72
normallap=71
holeshotindex=1
time=0
laps=6
starttime=1846
date=1407162774
players:
slot=0
uid=8240
number=172
bike=rm125
name=Maximilien Jannot | RH-Factory
slot=1
uid=7910
number=666
bike=rm125
name=Patrick Corvisier|Team RH-Factory
slot=2
uid=10380
number=114
bike=rm125
name=Benoit Krawiec | MXS-Concept.com
slot=6
uid=6037
number=59
bike=rm125
name=Yohan Levrage | SPEED
slot=8
uid=6932
number=447
bike=rm125
name=Morgan Marlet | Mxs-Concept.com
times:
6 0 1974
1 0 1989
0 0 2020
2 0 2056
6 1 2242
1 1 2260
0 1 2313
2 1 2338
6 2 2434
1 2 2452';

$results = array();

$parseResults = false;

#foreach($lines as $line){ // use this line when working with file_get_contents

foreach(preg_split("/((\r?\n)|(\r\n?))/", $lines) as $line){     

    if($parseResults === true) {
        $parts = explode(' ', $line); // SLOT|GATE|TIME = parts 0|1|2
        $times = array(
            'slot' => $parts[0],
            'gate' => $parts[1],
            'time' => $parts[2]
        );
        $results['times'][] = $times;
    }

    if(false !== strpos($line, '=')) { // if line has a = in it, explode it
        $parts = explode('=', $line);    
        $results[$parts[0]] = $parts[1]; // assign parts to array as key=value
    }

    if(false !== strpos($line, 'times:')) {
        // we reached "times:", let's set a flag to start reading results in the next iteration
        $parseResults = true;
    }
}

var_dump($results);

输出:

array(15) { 
["dir"]=> string(15) "Dardon-Gueugnon" 
["longname"]=> string(15) "Dardon Gueugnon" 
....
["name"]=> string(31) "Morgan Marlet | Mxs-Concept.com" 
["times"]=> array(10) { 
    [0]=> array(3) { ["slot"]=> string(1) "6" ["gate"]=> string(1) "0" ["time"]=> string(4) "1974" } 
    [1]=> array(3) { ["slot"]=> string(1) "1" ["gate"]=> string(1) "0" ["time"]=> string(4) "1989" } 
    [2]=> array(3) { ["slot"]=> string(1) "0" ["gate"]=> string(1) "0" ["time"]=> string(4) "2020" } 
...
} } }