文件将多个请求中的内容放入特定格式的PHP中
我想将2个或更多请求中的内容放入一个文件,该文件的格式如下:文件将多个请求中的内容放入特定格式的PHP中,php,file-put-contents,Php,File Put Contents,我想将2个或更多请求中的内容放入一个文件,该文件的格式如下: customer | invoice 下面我设法将这两个请求都发布了,但如何将它们以以下格式发布: Customer1 | Invoice1 Customer2 | Invoice2 ..etc PHP代码: file_put_contents('/root/service_Logs/service.log'," {$this->request->cu
customer | invoice
下面我设法将这两个请求都发布了,但如何将它们以以下格式发布:
Customer1 | Invoice1
Customer2 | Invoice2 ..etc
PHP代码:
file_put_contents('/root/service_Logs/service.log',"
{$this->request->customer}\n",FILE_APPEND);
file_put_contents('/root/service_Logs/service.log',
"{$this->request->invoice}\n",FILE_APPEND);
编辑:苹果仍然无法获得这种格式。有什么想法吗?一种方法:
$file = '/root/service_Logs/service.log';
$text = sprintf('%s | %s', $this->request->customer, $this->request->invoice);
file_put_contents($file, $text . PHP_EOL, FILE_APPEND);
我之所以使用它,是因为它允许我将字符串的格式与数据分开指定为该格式
您也可以使用插值,但在我看来,这更复杂:
$text = "{$this->request->customer} | {$this->request->invoice}";
但是,无论您做什么,都要努力将变量声明与使用这些变量的逻辑分开。当你把所有的事情都打包在一个电话里时,它会变得冗长、混乱,而且很难维护。即使对于日志语句这样的废弃行,这也是一个坏习惯。我正在尝试为每个条目添加一个时间戳。我使用了下面的代码:$text=sprintf(日期('Y-m-dh:I:s')。“-”'%s |%s',$this->request->customer,$this->request->invoice);但输出结果却不走运。我做错了什么?
$text=sprintf('%s%s |%s',date(…),…)代码>