5行目が帯域幅であるため、大きなファイルの5行目のカウントにはどのようなPHP関数を使用する必要がありますか?大きなファイルの5行目を読むにはどのような機能を使用しますか?
データの例:
103.239.234.105 -- [2007-04-01 00:42:21] "GET articles/learn_PHP_basics HTTP/1.0" 200 12729 "Mozilla/4.0"
5行目が帯域幅であるため、大きなファイルの5行目のカウントにはどのようなPHP関数を使用する必要がありますか?大きなファイルの5行目を読むにはどのような機能を使用しますか?
データの例:
103.239.234.105 -- [2007-04-01 00:42:21] "GET articles/learn_PHP_basics HTTP/1.0" 200 12729 "Mozilla/4.0"
オープンハンドルにファイル:
$handle = fopen("someFilePath", "r");
そして最初の5行を読み取り、そしてのみ第五の保存:
$i = 0;
$fifthLine = null;
while (($buffer = fgets($handle, 4096)) !== false) {
if $i++ >= 5) {
$fifthLine = $buffer;
break;
}
}
fclose($handle);
if ($i < 5); // uh oh, there weren't 5 lines in the file!
//$fifthLine should contain the 5th line in the file
注これはストリーミングされているので、ファイル全体がロードされません。
あなたはごとに5番目の行を読みたい場合は、(機能のfopen/fgets/fclose
家族より)生活が少し楽にするためにSplFileObject
を使用することができます。
$f = new SplFileObject('myreallybigfile.txt');
// Read ahead so that if the last line in the file is a 5th line, we echo it.
$f->setFlags(SplFileObject::READ_AHEAD);
// Loop over every 5th line starting at line 5 (offset 4).
for ($f->rewind(), $f->seek(4); $f->valid(); $f->seek($f->key()+5)) {
echo $f->current();
}
Splファイル関数は、行く方法です。 –
大きなファイルの5行目ごとに帯域幅を数えたい - データの例:103.239.234.105 - [2007-04-01 00:42:21] "GET articles/learn_PHP_basics HTTP/1.0" 2002729 "Mozilla/4.0" - – Sky12
Sky12は、Apacheのaccess_log行を解析するためのstackoverflow(とGoogle!)の周りを見てください。オプションは、 'explode()'から、必要な情報を抽出するために正規表現を使用して、多くの既存の関数の1つを使用して必要な情報を取得することに行きます。それは簡単な部分です。 – salathe
http://tekkie.flashbit.net/php/tail-functionality-in-php
<?php
// full path to text file
define("TEXT_FILE", "/home/www/default-error.log");
// number of lines to read from the end of file
define("LINES_COUNT", 10);
function read_file($file, $lines) {
//global $fsize;
$handle = fopen($file, "r");
$linecounter = $lines;
$pos = -2;
$beginning = false;
$text = array();
while ($linecounter > 0) {
$t = " ";
while ($t != "\n") {
if(fseek($handle, $pos, SEEK_END) == -1) {
$beginning = true;
break;
}
$t = fgetc($handle);
$pos --;
}
$linecounter --;
if ($beginning) {
rewind($handle);
}
$text[$lines-$linecounter-1] = fgets($handle);
if ($beginning) break;
}
fclose ($handle);
return array_reverse($text);
}
$fsize = round(filesize(TEXT_FILE)/1024/1024,2);
echo "<strong>".TEXT_FILE."</strong>\n\n";
echo "File size is {$fsize} megabytes\n\n";
echo "Last ".LINES_COUNT." lines of the file:\n\n";
$lines = read_file(TEXT_FILE, LINES_COUNT);
foreach ($lines as $line) {
echo $line;
}
今後詳細を記入してください。リンクのみを含む回答は、ウェブサイトが移動したりコンテンツが変更されたりするため、あまり役に立たない傾向があります。 – jwiscarson
あなたは、ファイルがどのように見える、とあなたはあなたが得る結果と何をしたいのか何の例を投稿することができますか?あなたは、テキストファイルの5行目の整数を読み込もうとしていますか、あるいはその線上にあるテキストを送信するために必要な帯域幅は、有線を介して読もうとしていますか? –
ファイルの5行目、または* 5行目ごとに読みたいですか?ファイルの(短い)例と読みたいものを与えてください。 – salathe
データの例:103.239.234.105 - [2007-04-01 00:42:21] "GET articles/learn_PHP_basics HTTP/1.0" 2002729大きなファイルの5行目ごとに帯域幅を数えたい"Mozilla/4.0" – Sky12