Skip to content

Instantly share code, notes, and snippets.

@marihachi
Last active December 26, 2016 12:53
Show Gist options
  • Save marihachi/67eb195bc67c4ce45c91861cd10103b0 to your computer and use it in GitHub Desktop.
Save marihachi/67eb195bc67c4ce45c91861cd10103b0 to your computer and use it in GitHub Desktop.
テキストファイルの最初の行を削除して保存しなおすサンプル
<?php
$data = [];
// 読み込み
$handle = fopen("hoge.txt", "r");
if ($handle) {
while (($line = fgets($handle, 4096)) !== false) {
array_push($data, $line);
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
// data の最初の要素を削除
unset($data[0]);
$data = array_values($data);
// 書き込み
$handle = fopen("hoge.txt", "w");
if ($handle) {
foreach ($data as $line) {
fputs($handle, $line);
}
fclose($handle);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment