fgetsがここでの答えだと思います。
$handle = fopen($path, 'r+'); // open the file for r/w
while (!feof($handle)) { // while not end
$value = trim(fgets($handle)); // get the trimmed line
if ($value == $input) { // is it the value?
return; // if so, bail out
} //
} // otherwise continue
fwrite($handle, $input); // hasn't bailed, good to write
fclose($handle); // close the file
"\n"
この回答は、コードに改行 ( ) を追加したという事実のみに基づいているため、fgets
ここで機能します。file_get_contents()
これは、単にファイルのサイズがそれを制限している可能性があるため、 でファイル全体をメモリにプルするよりも望ましい場合があります。
あるいは、値が改行で区切られておらず、固定長である場合は、 の$length
引数を使用fgets()
して正確に$n
文字を取り出すことができます (または を使用fread()
して正確に$n
バイトを取り出すことができます) 。