Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP将段落拆分为多行_Php - Fatal编程技术网

使用PHP将段落拆分为多行

使用PHP将段落拆分为多行,php,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

我有一个长字符串,如下所示:

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的屏幕截图一样。 请点击此链接查看屏幕截图

我已经用下面的expolde函数进行了尝试,但我无法得到结果

$result=“偏移量(V)名称PID PPID Thds Hnds Sess Wow64开始退出0x819cc830系统4 0 51 254-1 0 0x817e4670 smss.exe 360 4 19-1 0 2008-11-26 07:38:11 UTC+0000 0x81BD78 csrss.exe 596 360 10 3220 2008-11-26 07:38:13 UTC+0000 0x8182b100 winlogon.exe 620 360 16 503 0 2008-11-26 07:38:14 UTC+0000 0x8183ba78 services.exe 672 620 15 2450 2008-11-26 07:38:15:15UTC+0000 0x817dbc30 lsass.exe 684 620 21 347 0 2008-11-26 07:38:15 UTC+0000 0x81859d70 svchost.exe 844 672 19 198 0 2008-11-26 07:38:18 UTC+0000 0x8183d360 svchost.exe 932 672 10 229 0 2008-11-26 07:38:18 UTC+0000 0x818a2300 svchost.exe 1064 672 63 1308 0 2008-11-11-26 07:38:20 UTC+0000 0x817f7da0 svchost.exe 1160 2008-11:77:2638:23 UTC+0000 0x8180e6f0 svchost.exe 1264 672 14 209 0 2008-11-26 07:38:25 UTC+0000 0x817ca478 explorer.exe 1516 1452 12 362 0 2008-11-26 07:38:27 UTC+0000 0x816e75e8 UTC spoolsv.exe 1648 672 12 112 0 2008-11-26 07:38:28 UTC+0000 0x816af860 VMwareTray.exe 1896 1516 1 26 0 2008-11-26 07:38:31+0000 0x816af448 VMwareUser 1904 15100 2008-11-26 07:38:31 UTC+0000 0x816a13c0 VMwareService.e 1756 672 3 45 0 2008-11-26 07:38:45 UTC+0000 0x816557e0 alg.exe 512 672 6 105 00 2008-11-26 07:38:53 UTC+0000 0x81643b28 Wauclt.exe 1372 1064 8 225 0 2008-11-26 07:39:38 UTC+0000 0x8164E38 wscntfy.exe 560 1064 1 31 0 2008-11-26 07:44+0000”

$rows=分解(“,$result);
当我打印r($rows)时;数组中有一些空元素,如果有人能帮我编写代码,我将不胜感激

您应该寻找的第一件事是某种将数据分隔成行的方法:

$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';    
因为没有标准的,比如换行符或分号之类的,所以你必须想出别的办法。从我所看到的,您应该能够在
偏移量(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
date(或只添加一个空列):

据我所见,字符串中的任何位置都没有为
Exit
列设置值,因此我们只将其设置为空值。此时可以设置的唯一方法是,如果我们正在处理的行是标题行。(如果您在上面提供的小来源中缺少此行为,则当然可以更改此行为)

现在,我们可以使用

完整源代码:

// 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;

那么你的问题是什么?在你说“我该怎么做?”之前,请记住,像这样的问题太宽泛了,因此将被关闭。因此,如果您已经编写了任何代码,我强烈建议将其添加到您的问题中。谢谢您的建议。我已经提到了我想要的。谢谢你。解释得很好。但是我有一个问题,当我在localhost(xampp)上运行这个时,我没有得到预期的输出,但是我在这里运行相同的代码,我可以得到预期的输出。我想这是因为“vprintf”。我的本地主机php版本是5.5.9[Apache版本Apache/2.4.7(Win32)OpenSSL/1.0.1e php/5.5.9]。请让我知道如何解决这个问题。谢谢您在本地运行时得到了什么输出?在沙盒服务器上运行时有什么区别?您是否有任何错误?这是我在localhost上运行它时得到的结果-但在sandbox服务器上,我得到的预期输出与您在第一次回答中提到的输出相同。哦,您需要将输出包装为一些HTML,以便能够在web浏览器中正确查看它(没有sandbox使用的文本区域)。请参阅更新的答案以获得快速修复。我正在获得结果。你救了我谢谢:)
        @$cols[9] = $cols[9] ?: '';
        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