PHP - 尝试重写文件但留下空白
PHP - Trying to re-write a file and it's leaving blank spaces
您好,我在重写文件时遇到问题:
static function Remove($plate){
$_parkedlist=parking::Read();
$_remove = false;
$_stillparkedlist = array();
foreach($_parkedlist as $_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 as $_car){
if($_car[0]!=""){
$_line = $_car[0]."=>".$_car[1]."\n";
fwrite($mifile,$_line);
}
}
fclose($mifile);
}
}
原文件是这样的:
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
我第一次删除它时添加了空格(删除了 706KHB)
234FSC=>2016-09-07 17:06:23
JAG823=>2016-09-07 17:06:15
980GHB=>2016-09-07 17:06:15
如果我再次删除它会开始显示 "Notice: Undefined offset: 1 in ...\parking.php" 并且文件看起来像这样
234FSC=>2016-09-07 17:06:23
=>
JAG823=>2016-09-07 17:06:15
=>
980GHB=>2016-09-07 17:06:15
=>
我已经尝试了所有我能找到的,但即使使用 if($_car[0]!="") 和 isset($_car[0]!)
我怎样才能重写而不出现这个错误?
我假设从文件中读取的每一行都已经附加了换行符。
所以当你执行这一行时
$_line = $_car[0]."=>".$_car[1]."\n";
它正在添加另一个换行符,所以改为
$_line = $_car[0]."=>".$_car[1];
所以你没有添加另一个换行符
发生的事情是 fwrite
已经添加了一个 PHP_EOL
,所以删除额外的 "\n"
然而,也许您应该重组数组以使事情变得更简单:
static function Remove($plate){
$data = parking::Read();
// restructuring happens here.
foreach($data as $car){
$list[$car[0]] = $car[1];
}
foreach($list as $key => $val){
if($key == $plate){
$now = date('Y-m-d H:i:s');
$elapsed = strtotime($now) - strtotime($val);
$price = $elapsed * 10;
echo $price;
unset($list[$key]); // *poof*, removed the found car from the list.
break;
}
}
// if the new generated list is smaller then the original one, update.
if(sizeof($list) != sizeof($data)){
$fh = fopen('parked.txt',"w");
foreach($list as $key => $val){
fwrite($fh,"$key=>$plate");
}
fclose($fh);
}
}
这段代码还有一个怪癖,它会自动删除重复的条目。
您好,我在重写文件时遇到问题:
static function Remove($plate){
$_parkedlist=parking::Read();
$_remove = false;
$_stillparkedlist = array();
foreach($_parkedlist as $_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 as $_car){
if($_car[0]!=""){
$_line = $_car[0]."=>".$_car[1]."\n";
fwrite($mifile,$_line);
}
}
fclose($mifile);
}
}
原文件是这样的:
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
我第一次删除它时添加了空格(删除了 706KHB)
234FSC=>2016-09-07 17:06:23
JAG823=>2016-09-07 17:06:15
980GHB=>2016-09-07 17:06:15
如果我再次删除它会开始显示 "Notice: Undefined offset: 1 in ...\parking.php" 并且文件看起来像这样
234FSC=>2016-09-07 17:06:23
=>
JAG823=>2016-09-07 17:06:15
=>
980GHB=>2016-09-07 17:06:15
=>
我已经尝试了所有我能找到的,但即使使用 if($_car[0]!="") 和 isset($_car[0]!)
我怎样才能重写而不出现这个错误?
我假设从文件中读取的每一行都已经附加了换行符。
所以当你执行这一行时
$_line = $_car[0]."=>".$_car[1]."\n";
它正在添加另一个换行符,所以改为
$_line = $_car[0]."=>".$_car[1];
所以你没有添加另一个换行符
发生的事情是 fwrite
已经添加了一个 PHP_EOL
,所以删除额外的 "\n"
然而,也许您应该重组数组以使事情变得更简单:
static function Remove($plate){
$data = parking::Read();
// restructuring happens here.
foreach($data as $car){
$list[$car[0]] = $car[1];
}
foreach($list as $key => $val){
if($key == $plate){
$now = date('Y-m-d H:i:s');
$elapsed = strtotime($now) - strtotime($val);
$price = $elapsed * 10;
echo $price;
unset($list[$key]); // *poof*, removed the found car from the list.
break;
}
}
// if the new generated list is smaller then the original one, update.
if(sizeof($list) != sizeof($data)){
$fh = fopen('parked.txt',"w");
foreach($list as $key => $val){
fwrite($fh,"$key=>$plate");
}
fclose($fh);
}
}
这段代码还有一个怪癖,它会自动删除重复的条目。