从PHP文件中提取数据

时间:2023-02-04 10:44:40

I have this array $awstat that I extacted from an awstat file, an withing $awstat I have this that I need:

我有一个这个数组$ awstat,我从一个awstat文件中提取,一个带有$ awstat我有这个我需要的:

BEGIN_TIME 24
0 3245 9955 143463426 8047 13601 475741423
1 3122 9131 146244440 7579 12936 507921700
2 2639 5706 95369716 7351 11987 490330698
3 1917 4062 79234871 8245 13009 579453498
4 1757 4263 65580607 7887 11437 454870321
5 1723 4022 44682383 6888 10263 326819624
6 1876 4677 56964771 7339 11242 355385677
7 2796 8473 120152521 7770 12176 362904239
8 4227 13791 196173677 7421 12196 366706352
9 7984 25965 375376297 8398 13883 406545549
10 14605 34418 434054375 7183 13341 380773129
11 15533 41259 559938996 7123 12690 372426426
12 17495 40043 505139834 7432 13402 518541077
13 15815 34170 385108531 6519 12390 396494926
14 16330 41073 508838859 6761 12318 348417806
15 19093 44058 483568307 7692 13583 454365520
16 30429 59672 577852398 8273 13231 473134295
17 25094 48897 478246556 8207 12898 476038603
18 19136 42665 482073005 8087 12983 468300958
19 28849 46228 371229572 7721 12688 471632281
20 14068 30981 341103557 7832 13251 417443822
21 14727 33458 394841797 7575 12644 388811384
22 13480 31364 365096742 7460 13114 411771572
23 7189 19744 272606100 6643 12398 397762547
END_TIME

So I tried this and it doesn't seem to work!

所以我尝试了这个似乎不起作用!

preg_match("/BEGIN_TIME(.*)END_TIME/is", $awstats, $matches);
$time = $matches[0] ; 
var_dump($time); // it displays "NULL"

Any solution for this? Thanks!

对此有何解决方案?谢谢!

3 个解决方案

#1


1  

Not so much an answer, more of an opinion.

不是一个答案,而是更多的意见。

Using regular expressions here is overkill.

在这里使用正则表达式是过度的。

Something as simple as this will do:

像这样简单的事情会做:

$lines = file("awstats.output");
$lines = array_slice(1); // remove first line
$lines = array_slice(0, -1); // remove last line
foreach ($lines as $line) {
    $data = explode(" ", $line);
    // handle data
}

#2


0  

You can match linebreaks with:

您可以将换行符与以下内容匹配:

([^\n]*\n+)+

So this should work:

所以这应该工作:

preg_match("/BEGIN_DAY([^\n]*\n+)+END_DAY/is", $awstats, $matches);

#3


0  

$time = file_get_contents('awstat.log');    
$time = preg_replace('/BEGIN_TIME(.*?)END_TIME/sim', '$1', $time);

#1


1  

Not so much an answer, more of an opinion.

不是一个答案,而是更多的意见。

Using regular expressions here is overkill.

在这里使用正则表达式是过度的。

Something as simple as this will do:

像这样简单的事情会做:

$lines = file("awstats.output");
$lines = array_slice(1); // remove first line
$lines = array_slice(0, -1); // remove last line
foreach ($lines as $line) {
    $data = explode(" ", $line);
    // handle data
}

#2


0  

You can match linebreaks with:

您可以将换行符与以下内容匹配:

([^\n]*\n+)+

So this should work:

所以这应该工作:

preg_match("/BEGIN_DAY([^\n]*\n+)+END_DAY/is", $awstats, $matches);

#3


0  

$time = file_get_contents('awstat.log');    
$time = preg_replace('/BEGIN_TIME(.*?)END_TIME/sim', '$1', $time);