fwrite - PHP - Trying to re-write a file and it's leaving blank spaces -
hello have issue when re-writting file:
static function remove($plate){ $_parkedlist=parking::read(); $_remove = false; $_stillparkedlist = array(); foreach($_parkedlist $_car){ if($_car[0] == $plate){ $_firsttime = $_car[1]; $_now = date('y-m-d h:i:s'); $_timelapse = strtotime($_now) - strtotime($_firsttime); $_topay = $_timelapse * 10; echo "$_topay <br>"; $_remove = true; } else { $_stillparkedlist [] = $_car; } } if ($_remove == true){ $mifile = fopen('parked.txt',"w"); foreach($_stillparkedlist $_car){ if($_car[0]!=""){ $_line = $_car[0]."=>".$_car[1]."\n"; fwrite($mifile,$_line); } } fclose($mifile); } }
the original file this:
234fsc=>2016-09-07 17:06:23 jag823=>2016-09-07 17:06:15 706khb=>2016-09-07 17:06:15 980ghb=>2016-09-07 17:06:15
the first time remove adds blank spaces (removed 706khb)
234fsc=>2016-09-07 17:06:23 jag823=>2016-09-07 17:06:15 980ghb=>2016-09-07 17:06:15
if remove again starts showing "notice: undefined offset: 1 in ...\parking.php" , file looks this
234fsc=>2016-09-07 17:06:23 => jag823=>2016-09-07 17:06:15 => 980ghb=>2016-09-07 17:06:15 =>
i have tried find still happening if($_car[0]!="") , isset($_car[0]!)
how can re-write without error?
i assume each line read file newline attached it.
so when execute line
$_line = $_car[0]."=>".$_car[1]."\n";
it adding newline instead do
$_line = $_car[0]."=>".$_car[1];
so not adding newline
Comments
Post a Comment