0

有没有更好的方法来做到这一点?:

$dataString2 = " $leder klarte orgkrim å fikk: ".$showcah." den ".$timed." ";
$dataString3 = " $hacker klarte orgkrim å fikk: ".$showcah." den ".$timed." ";
$dataString4 = " $driver klarte orgkrim å fikk: ".$showcah." den ".$timed." ";
$dataString5 = " $weaponexpert klarte orgkrim å fikk: ".$showcah." den ".$timed." ";
$dataStringinfo = "$leder, $hacker, $driver, $weaponexpert klarte å OC med å få ".$showcah." tid: ".$timed.""; 
$dataStringinfo .= "\n";
$dataString2 .= "\n";
$dataString3 .= "\n";
$dataString4 .= "\n";
$dataString5 .= "\n";
$fWrite0 = fopen("/home/nordic/www/logger/orgkrim/completed/oversikt.txt","a");
$fWrite1 = fopen("/home/nordic/www/logger/orgkrim/oversikt.txt","a");
$fWrite2 = fopen("/home/nordic/www/logger/orgkrim/completed/$leder.txt","a");
$fWrite3 = fopen("/home/nordic/www/logger/orgkrim/completed/$hacker.txt","a");
$fWrite4 = fopen("/home/nordic/www/logger/orgkrim/completed/$driver.txt","a");
$fWrite5 = fopen("/home/nordic/www/logger/orgkrim/completed/$weaponexpert.txt","a");

$wrote2 = fwrite($fWrite2, $dataString2);
$wrote2 = fwrite($fWrite3, $dataString3);
$wrote3 = fwrite($fWrite4, $dataString4);
$wrote4 = fwrite($fWrite5, $dataString5);
$wrote5 = fwrite($fWrite1, $dataStringinfo);
$wrote0 = fwrite($fWrite0, $dataStringinfo);
fclose($fWrite0);
fclose($fWrite1);
fclose($fWrite2);
fclose($fWrite3);
fclose($fWrite4);
fclose($fWrite5);

简而言之,这会将各种“日志”发送到不同的地方。我想知道这里是否有更简单的方法可以节省空间。

4

2 回答 2

0

上面的评论表明使用了一个记录器类,但我认为对于这个孤立的例子来说,这将是矫枉过正。

请注意,您的大部分代码对每个不同的文件都执行相同的操作:fopen, fwrite, fclose. 这一系列函数调用将是转换为函数的理想候选者。幸运的是,该功能已经存在:file_put_contents.

$common = " klarte orgkrim å fikk: ".$showcah." den ".$timed." \n";

$dataString2 = " $leder" . $common;
$dataString3 = " $hacker" . $common;
$dataString4 = " $driver" . $common;
$dataString5 = " $weaponexpert" . $common;
$dataStringinfo = "$leder, $hacker, $driver, $weaponexpert klarte å OC med å få ".$showcah." tid: ".$timed."\n"; 

$logdir = "/home/nordic/www/logger/orgkrim/";

file_put_contents($logdir."/completed/oversikt.txt", $dataStringinfo, FILE_APPEND);
file_put_contents($logdir."/oversikt.txt", $dataStringinfo, FILE_APPEND);
file_put_contents($logdir."/completed/$leder.txt", $dataString2, FILE_APPEND);
file_put_contents($logdir."/completed/$hacker.txt", $dataString3, FILE_APPEND);
file_put_contents($logdir."/completed/$driver.txt", $dataString4, FILE_APPEND);
file_put_contents($logdir."/completed/$weaponexpert.txt", $dataString5, FILE_APPEND);

请注意,我通过简单地更改删除了许多行:

$foo = "bla".$bar." ";
$foo .= "\n";

$foo = "bla".$bar." \n";

于 2013-07-23T22:11:07.017 回答
0

尝试使用循环,例如:

$dsA[0] = $dsA[1] = "$leder, $hacker, $driver, $weaponexpert klarte å OC med å få $showcah tid: $timed \n";
$dsV = array($leder, $hacker, $driver, $weaponexpert);
for($i=2; $i<6; $i++){
  $dsA[$i] = " $dsV[$i] klarte orgkrim å fikk: $showcah den $timed \n";
}
$cp = 'completed';
$fwS = array('oversikt.txt', "$cp/oversikt.txt", "$cp/$leder.txt", "$cp/$hacker.txt", "$cp/$driver.txt", "$cp/$weaponexpert.txt");
foreach($fwS as $i => $v){
  $fp = fopen("/home/nordic/www/logger/orgkrim/$v", 'a');
  fwrite($fp, $dsA[$i]); fclose($fp);
}

对不起,我不会说你的语言。

于 2013-07-23T22:36:46.260 回答