使用PHP将段拆分为行

时间:2015-03-18 17:59:06

标签: php

我有一个长字符串如下:

Offset(V) Name PID PPID Thds Hnds Sess Wow64 Start Exit 0x819cc830 System 4 0 51 254 -1 0 0x817e4670 smss.exe 360 4 3 19 -1 0 2008-11-26 07:38:11 UTC+0000 0x8181bd78 csrss.exe 596 360 10 322 0 0 2008-11-26 07:38:13 UTC+0000 0x8182b100 winlogon.exe 620 360 16 503 0 0 2008-11-26 07:38:14 UTC+0000 0x8183ba78 services.exe 672 620 15 245 0 0 2008-11-26 07:38:15 UTC+0000 0x817dbc30 lsass.exe 684 620 21 347 0 0 2008-11-26 07:38:15 UTC+0000 0x81859d70 svchost.exe 844 672 19 198 0 0 2008-11-26 07:38:18 UTC+0000 0x8183d360 svchost.exe 932 672 10 229 0 0 2008-11-26 07:38:18 UTC+0000 0x818a2300 svchost.exe 1064 672 63 1308 0 0 2008-11-26 07:38:20 UTC+0000 0x817f7da0 svchost.exe 1164 672 5 77 0 0 2008-11-26 07:38:23 UTC+0000 0x8180e6f0 svchost.exe 1264 672 14 209 0 0 2008-11-26 07:38:25 UTC+0000 0x817ca478 explorer.exe 1516 1452 12 362 0 0 2008-11-26 07:38:27 UTC+0000 0x816e75e8 spoolsv.exe 1648 672 12 112 0 0 2008-11-26 07:38:28 UTC+0000 0x816af860 VMwareTray.exe 1896 1516 1 26 0 0 2008-11-26 07:38:31 UTC+0000 0x816af448 VMwareUser.exe 1904 1516 1 28 0 0 2008-11-26 07:38:31 UTC+0000 0x816a13c0 VMwareService.e 1756 672 3 45 0 0 2008-11-26 07:38:45 UTC+0000 0x816557e0 alg.exe 512 672 6 105 0 0 2008-11-26 07:38:53 UTC+0000 0x81643b28 wuauclt.exe 1372 1064 8 225 0 0 2008-11-26 07:39:38 UTC+0000 0x8164e3a8 wscntfy.exe 560 1064 1 31 0 0 2008-11-26 07:44:57 UTC+0000

我想把它分成正确对齐的行,就像下面的截图一样使用php。 请点击此链接查看屏幕截图http://screenshot.net/96ql5c4

我已尝试使用expolde函数,如下所示,但我无法得到结果

$ result =" Offset(V)名称PID PPID Thds Hnds Sess Wow64 Start Exit 0x819cc830 System 4 0 51 254 -1 0 0x817e4670 smss.exe 360​​ 4 3 19 -1 0 2008-11-26 07: 38:11 UTC + 0000 0x8181bd78 csrss.exe 596 360 10 322 0 0 2008-11-26 07:38:13 UTC + 0000 0x8182b100 winlogon.exe 620 360 16 503 0 0 2008-11-26 07:38:14 UTC +0000 0x8183ba78 services.exe 672 620 15 245 0 0 2008-11-26 07:38:15 UTC + 0000 0x817dbc30 lsass.exe 684 620 21 347 0 0 2008-11-26 07:38:15 UTC + 0000 0x81859d70 svchost .exe 844 672 19 198 0 0 2008-11-26 07:38:18 UTC + 0000 0x8183d360 svchost.exe 932 672 10 229 0 0 2008-11-26 07:38:18 UTC + 0000 0x818a2300 svchost.exe 1064 672 63 1308 0 0 2008-11-26 07:38:20 UTC + 0000 0x817f7da0 svchost.exe 1164 672 5 77 0 0 2008-11-26 07:38:23 UTC + 0000 0x8180e6f0 svchost.exe 1264 672 14 209 0 0 2008-11-26 07:38:25 UTC + 0000 0x817ca478 explorer.exe 1516 1452 12 362 0 0 2008-11-26 07:38:27 UTC + 0000 0x816e75e8 spoolsv.exe 1648 672 12 112 0 0 2008-11- 26 07:38:28 UTC + 0000 0x816af860 VMwareTray.exe 1896 1516 1 26 0 0 2008-11-26 07:38:31 UTC + 0000 0x816af448 VMwareUser.exe 1904 1516 1 28 0 0 2008-11-26 07:38:31 UTC + 0000 0x816a13c0 VMwareService.e 1756 672 3 45 0 0 2008-11 -26 07:38:45 UTC + 0000 0x816557e0 alg.exe 512 672 6 105 0 0 2008-11-26 07:38:53 UTC + 0000 0x81643b28 wuauclt.exe 1372 1064 8 225 0 0 2008-11-26 07: 39:38 UTC + 0000 0x8164e3a8 wscntfy.exe 560 1064 1 31 0 0 2008-11-26 07:44:57 UTC + 0000&#34 ;;

$ rows = explode("",$ result); 当我print_r($ rows);数组上有一些空元素,所以如果有人可以帮助我如何编码,我真的很感激。

1 个答案:

答案 0 :(得分:0)

您应该首先考虑的是将数据分成行的一些方法:

$source = 'Offset(V) Name PID PPID Thds Hnds Sess Wow64 Start Exit 0x819cc830 System 4 0 51 254 -1 0 0x817e4670 smss.exe 360 4 3 19 -1 0 2008-11-26 07:38:11 UTC+0000 0x8181bd78 csrss.exe 596 360 10 322 0 0 2008-11-26 07:38:13 UTC+0000 0x8182b100 winlogon.exe 620 360 16 503 0 0 2008-11-26 07:38:14 UTC+0000 0x8183ba78 services.exe 672 620 15 245 0 0 2008-11-26 07:38:15 UTC+0000 0x817dbc30 lsass.exe 684 620 21 347 0 0 2008-11-26 07:38:15 UTC+0000 0x81859d70 svchost.exe 844 672 19 198 0 0 2008-11-26 07:38:18 UTC+0000 0x8183d360 svchost.exe 932 672 10 229 0 0 2008-11-26 07:38:18 UTC+0000 0x818a2300 svchost.exe 1064 672 63 1308 0 0 2008-11-26 07:38:20 UTC+0000 0x817f7da0 svchost.exe 1164 672 5 77 0 0 2008-11-26 07:38:23 UTC+0000 0x8180e6f0 svchost.exe 1264 672 14 209 0 0 2008-11-26 07:38:25 UTC+0000 0x817ca478 explorer.exe 1516 1452 12 362 0 0 2008-11-26 07:38:27 UTC+0000 0x816e75e8 spoolsv.exe 1648 672 12 112 0 0 2008-11-26 07:38:28 UTC+0000 0x816af860 VMwareTray.exe 1896 1516 1 26 0 0 2008-11-26 07:38:31 UTC+0000 0x816af448 VMwareUser.exe 1904 1516 1 28 0 0 2008-11-26 07:38:31 UTC+0000 0x816a13c0 VMwareService.e 1756 672 3 45 0 0 2008-11-26 07:38:45 UTC+0000 0x816557e0 alg.exe 512 672 6 105 0 0 2008-11-26 07:38:53 UTC+0000 0x81643b28 wuauclt.exe 1372 1064 8 225 0 0 2008-11-26 07:39:38 UTC+0000 0x8164e3a8 wscntfy.exe 560 1064 1 31 0 0 2008-11-26 07:44:57 UTC+0000';    

由于没有标准的,如换行符或分号等,你必须想出其他的东西。从我所看到的,你应该能够打破Offset(V)十六进制值,这似乎从每一行开始(除了第一行)。所以我们分开来创建一个行数组:

$rows = preg_split('/(?=0x[\da-f]{8})/', $source);

我们现在可以循环遍历此数组,将数据解析为每行的列:

foreach ($rows as $row) {

通过拆分空格可以从行数据中创建列,但有一个例外; Start日期本身可能包含空格,因此会创建三个单独的列,这不是我们想要的。因此,我们将每一行分成两部分,一部分带有日期,另一部分带有其他列的数据:

    $dateRexEx = '/(.*?) (?:(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} \S+) ?)?$/';

    if (preg_match($dateRexEx, $row, $matches)) {

我们通过爆炸第一组数据来创建$cols数组:

        $cols = explode(' ', $matches[1]);

然后我们添加任何Start日期(或者只是一个空列):

        @$cols[] = $matches[2];

根据我的看法,字符串中的Exit列没有设置值,因此我们只需将其设置为空值。此时可以设置的唯一方法是我们正在处理的行是标题行。 (如果在上面提供的小源中缺少这种行为,当然可以改变这种行为)

        @$cols[9] = $cols[9] ?: '';

现在我们可以使用vprintf

显示格式化的输出
        vprintf('%-10s %-15s %4s %4s %4s %4s %4s %5s %-28s %-5s' . PHP_EOL, $cols);

完整源代码:

// Start HTML page
echo "<html><body><pre>", PHP_EOL;

// Define source
$source = 'Offset(V) Name PID PPID Thds Hnds Sess Wow64 Start Exit 0x819cc830 System 4 0 51 254 -1 0 0x817e4670 smss.exe 360 4 3 19 -1 0 2008-11-26 07:38:11 UTC+0000 0x8181bd78 csrss.exe 596 360 10 322 0 0 2008-11-26 07:38:13 UTC+0000 0x8182b100 winlogon.exe 620 360 16 503 0 0 2008-11-26 07:38:14 UTC+0000 0x8183ba78 services.exe 672 620 15 245 0 0 2008-11-26 07:38:15 UTC+0000 0x817dbc30 lsass.exe 684 620 21 347 0 0 2008-11-26 07:38:15 UTC+0000 0x81859d70 svchost.exe 844 672 19 198 0 0 2008-11-26 07:38:18 UTC+0000 0x8183d360 svchost.exe 932 672 10 229 0 0 2008-11-26 07:38:18 UTC+0000 0x818a2300 svchost.exe 1064 672 63 1308 0 0 2008-11-26 07:38:20 UTC+0000 0x817f7da0 svchost.exe 1164 672 5 77 0 0 2008-11-26 07:38:23 UTC+0000 0x8180e6f0 svchost.exe 1264 672 14 209 0 0 2008-11-26 07:38:25 UTC+0000 0x817ca478 explorer.exe 1516 1452 12 362 0 0 2008-11-26 07:38:27 UTC+0000 0x816e75e8 spoolsv.exe 1648 672 12 112 0 0 2008-11-26 07:38:28 UTC+0000 0x816af860 VMwareTray.exe 1896 1516 1 26 0 0 2008-11-26 07:38:31 UTC+0000 0x816af448 VMwareUser.exe 1904 1516 1 28 0 0 2008-11-26 07:38:31 UTC+0000 0x816a13c0 VMwareService.e 1756 672 3 45 0 0 2008-11-26 07:38:45 UTC+0000 0x816557e0 alg.exe 512 672 6 105 0 0 2008-11-26 07:38:53 UTC+0000 0x81643b28 wuauclt.exe 1372 1064 8 225 0 0 2008-11-26 07:39:38 UTC+0000 0x8164e3a8 wscntfy.exe 560 1064 1 31 0 0 2008-11-26 07:44:57 UTC+0000';

// Split rows on Offset value
$rows = preg_split('/(?=0x[\da-f]{8})/', $source);

foreach ($rows as $row) {
    // Split row data on "Start date"
    $dateRexEx = '/(.*?) (?:(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} \S+) ?)?$/';

    if (preg_match($dateRexEx, $row, $matches)) {
        // Make columns by splitting on space, except for "Start" date column,
        // which needs special treatment as it contains spaces in itself
        $cols = explode(' ', $matches[1]);

        // Add any "Start" date or set an empty column
        @$cols[] = $matches[2];

        // Add an empty "Exit" value unless set (i.e. it is the title)
        @$cols[9] = $cols[9] ?: '';

        // Format output
        vprintf('%-10s %-15s %4s %4s %4s %4s %4s %5s %-28s %-5s' . PHP_EOL, $cols);
    }
}

// End HTML page
echo "</pre></body></html>", PHP_EOL;

输出:

Offset(V)  Name             PID PPID Thds Hnds Sess Wow64 Start                        Exit 
0x819cc830 System             4    0   51  254   -1     0                                   
0x817e4670 smss.exe         360    4    3   19   -1     0 2008-11-26 07:38:11 UTC+0000      
0x8181bd78 csrss.exe        596  360   10  322    0     0 2008-11-26 07:38:13 UTC+0000      
0x8182b100 winlogon.exe     620  360   16  503    0     0 2008-11-26 07:38:14 UTC+0000      
0x8183ba78 services.exe     672  620   15  245    0     0 2008-11-26 07:38:15 UTC+0000      
0x817dbc30 lsass.exe        684  620   21  347    0     0 2008-11-26 07:38:15 UTC+0000      
0x81859d70 svchost.exe      844  672   19  198    0     0 2008-11-26 07:38:18 UTC+0000      
0x8183d360 svchost.exe      932  672   10  229    0     0 2008-11-26 07:38:18 UTC+0000      
0x818a2300 svchost.exe     1064  672   63 1308    0     0 2008-11-26 07:38:20 UTC+0000      
0x817f7da0 svchost.exe     1164  672    5   77    0     0 2008-11-26 07:38:23 UTC+0000      
0x8180e6f0 svchost.exe     1264  672   14  209    0     0 2008-11-26 07:38:25 UTC+0000      
0x817ca478 explorer.exe    1516 1452   12  362    0     0 2008-11-26 07:38:27 UTC+0000      
0x816e75e8 spoolsv.exe     1648  672   12  112    0     0 2008-11-26 07:38:28 UTC+0000      
0x816af860 VMwareTray.exe  1896 1516    1   26    0     0 2008-11-26 07:38:31 UTC+0000      
0x816af448 VMwareUser.exe  1904 1516    1   28    0     0 2008-11-26 07:38:31 UTC+0000      
0x816a13c0 VMwareService.e 1756  672    3   45    0     0 2008-11-26 07:38:45 UTC+0000      
0x816557e0 alg.exe          512  672    6  105    0     0 2008-11-26 07:38:53 UTC+0000      
0x81643b28 wuauclt.exe     1372 1064    8  225    0     0 2008-11-26 07:39:38 UTC+0000      
0x8164e3a8 wscntfy.exe      560 1064    1   31    0     0 2008-11-26 07:44:57 UTC+0000