次のコードで TXT ファイルを読み取り、各行から不要な情報を取り出し、編集した行を新しい TXT ファイルに保存します。
<?php
$file_handle = fopen("old.txt", "rb");
ob_start();
while (!feof($file_handle) ) {
$line_of_text = fgets($file_handle);
$parts = explode('\n', $line_of_text);
foreach ($parts as $str) {
$str_parts = explode('_', $str); // Split string by _ into an array
array_pop($str_parts); // Remove last element
array_shift($str_parts); // Remove first element
echo implode('_', $str_parts)."\n"; // Put it back together (and echo newline)
}
}
$new_content = ob_get_clean();
file_put_contents("new.txt", $new_content);
fclose($file_handle);
?>
新しい行が保存されるたびに 1 秒ずつ増加する $hr #min および $sec 変数を挿入したいと考えています。私の行が次のように読めるとしましょう(古いコード):
958588
978567
986766
新しいコードを次のようにしたい:
125959958588
130000978567
130001986766
ご覧のとおり、時間は 24 時間形式 (00 - 23) で、その後に分 (00 - 59)、秒 (00 - 59) が続き、最後に抽出された txt があります。
変数フレームワークを作成しましたが、変数を適切にインクリメントする方法がわかりません。誰か助けてくれませんか?
<?php
$file_handle = fopen("old.txt", "rb");
$hr = 00;
$min = 00;
$sec = 00;
ob_start();
while (!feof($file_handle) ) {
$line_of_text = fgets($file_handle);
$parts = explode('\n', $line_of_text);
foreach ($parts as $str) {
$str_parts = explode('_', $str); // Split string by _ into an array
array_pop($str_parts); // Remove last element
array_shift($str_parts); // Remove first element
echo $hr.$min.$sec.implode('_', $str_parts)."\n"; // Put it back together (and echo newline)
}
}
$new_content = ob_get_clean();
file_put_contents("new.txt", $new_content);
fclose($file_handle);
?>