次のような形式の txt ファイルがあります。
string1 value
string2 value
string3 value
外部スクリプトから変更された「値」を解析する必要がありますが、stringX は静的です。各行で値を取得するにはどうすればよいですか?
それはあなたのために働くはずです。
$lines = file($filename);
$values = array();
foreach ($lines as $line) {
if (preg_match('/^string(\d+) ([A-Za-z]+)$/', $line, $matches)) {
$values[$matches[1]] = $matches[2];
}
}
print_r($values);
これはあなたを助けることができます。一度に 1 行ずつ読み取り、Text.txt に 1000 行が含まれている場合でもfile_put_contents
、のように毎回実行すると、ファイルは行file_put-contents("result.txt", $line[1])
を読み取るたびに更新されます(または必要なアクションが実行されます) 。 1000 行すべてを読み取ります。そして、いつでも、メモリ内には 1 行しかありません。
<?php
$fp = fopen("Text.txt", "r") or die("Couldn't open File");
while (!feof($fp)) { //Continue loading strings till the end of file
$line = fgets($fp, 1024); // Load one complete line
$line = explode(" ", $line);
// $line[0] equals to "stringX"
// $line[1] equals to "value"
// do something with $line[0] and/or $line[1]
// anything you do here will be executed immediately
// and will not wait for the Text.txt to end.
} //while loop ENDS
?>