Last active
December 26, 2016 12:53
-
-
Save marihachi/67eb195bc67c4ce45c91861cd10103b0 to your computer and use it in GitHub Desktop.
テキストファイルの最初の行を削除して保存しなおすサンプル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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