仅从文件目录中计算文章

时间:2011-11-06 18:19:15

标签: php

我正在尝试计算大文件中的文章数量 - 例如 -

103.239.234.105 -- [2007-04-01 00:42:21] "GET articles/learn_PHP_basics HTTP/1.0" 200 12729 "Mozilla/4.0"
207.3.35.52 -- [2007-04-01 01:24:42] "GET index.php HTTP/1.0" 200 11411 "Mozilla/4.0"
51.4.190.113 -- [2007-04-01 02:07:04] "GET articles/php_classes_and_oop HTTP/1.0" 200 7674 "MSIE 7.0"

他们从哪里开始思考?

2 个答案:

答案 0 :(得分:1)

逐行读取文件并匹配articles/目录......:

$fp = fopen('mylogfile.log', 'r');
$articles = 0;
while ($line = fgets($fp)) {
   if (strpos($line, 'GET articles/') !== false) $articles++;
}
fclose($fp);

答案 1 :(得分:0)

<?php
$fileContent = file('path/to/file.log');
$articleToFind = 'learn_PHP_basics'; // exact phrase
$counter = 0;

foreach ($fileContent as $line) {
    if (FALSE !== strpos($line, $articleToFind)) {
        $counter++;
    }
}

echo "Search result for '$articleToFind': $counter";