| ||||||||||||||||||||||||
|
|
![]() Removal{Distance} of a line from a file I very much often meet a question such as " How to remove the certain line from a file? ". People at all does not want to move, in fact so it is easy to go on any large forum on PHP, and in term of search to write something such as " Removal{Distance} of lines from a file ". The five of results is guaranteed. So, we shall just consider a way of removal{distance} of the certain line from a text file It would be for the beginning completely not bad to create a text file. We shall name it file.txt which will consist of lines which we shall delete: Line one Line two Line three Line four Line five Then we create something such as file.php, borrowing{occupying} about{near} 11 lines! <? if ($id! = " ") { $id-; $file=file("file.txt "); for ($i=0; $i <sizeof ($file); $i ++) if ($i == $id) unset ($file [$i]); $fp=fopen ("file.txt", "w"); fputs ($fp, implode (" ", $file)); fclose ($fp); } ?> First we shall be defined{determined}, that its{her} number{room} is necessary for removal{distance} of the certain line for us. We shall pass it with the identifier id: file.php? id=2 (shall remove a line at number{room} THREE as calculations of elements in a file it is conducted from zero, but later we shall smooth it). ? Function file () reads out all file in a file (each line is his element); ? $id-subtract one of our identifier so 2 becomes 2, and 3 - 3; ? unset ($file [$i]) deletes or nulls an element of a file (but only from memory, i.e. it is virtual:); ? implode () unites a file in one line. It is very important, as if it have lowered{omitted}, our file would grow thin before useless recording Array; |
|||||||||||||||||||||||