SplFileObject::seek

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

SplFileObject::seekファイルポインタを指定行に移動させる

説明

public SplFileObject::seek(int $line): void

ファイルポインタを指定行に移動させます。

パラメータ

line

ゼロを起点とした移動させる行数。

戻り値

値を返しません。

エラー / 例外

line が負の値の場合、LogicException がスローされます。

例1 SplFileObject::seek() の例

この例はポジション 2 で見つかるスクリプトの第 3 行目を出力します。

<?php
$file
= new SplFileObject(__FILE__);
$file->seek(2);
echo
$file->current();
?>

上の例の出力は、 たとえば以下のようになります。

$file->seek(2);

参考

add a note add a note

User Contributed Notes 1 note

up
3
ahmad_maqsood at waku-2 dot com
6 years ago
A sleek way of counting the number of lines in a file can be like below

$file = new \SplFileObject('file.extension', 'r');
$file->seek(PHP_INT_MAX);

echo $file->key() + 1;
To Top