php - Get Last row value and N row value before last row -
how read , echo n rows before last row in text file using php?
example in file.txt
test1 test2 test3 test4 test5 test6 test7
i want last row value , 3 row before last row. result :
test4 test7
here code far (just show last row)
$line = ''; $f = fopen('\\\\192.168.183.28\\wk$\\sbin\\file.txt', 'r'); $cursor = -1; fseek($f, $cursor, seek_end); $char = fgetc($f); while ($char === "\n" || $char === "\r") { fseek($f, $cursor--, seek_end); $char = fgetc($f); } while ($char !== false && $char !== "\n" && $char !== "\r") { $line = $char . $line; fseek($f, $cursor--, seek_end); $char = fgetc($f); } $future_sn = substr($line, 28, 36);
any advice?
try
$array = explode("\n", file_get_contents('file.txt')); // counting array , key prev $prev_third = count($array)-4; echo end($array); // test7 echo $array[$prev_third]; //test4
Comments
Post a Comment