无法将字符串与 file_get_contens 在 PHP 中检索到的另一个字符串连接起来
Cannot concatenate string with another string retrieved by file_get_contens in PHP
所以我得到了一个打印以下日志的应用程序:
{"date":"15/09/2016", "time":"09:29:58","temp":"17.0", "humidity":"95.0" },
{"date":"15/09/2016", "time":"09:30:01","temp":"17.0", "humidity":"95.0" },
{"date":"15/09/2016", "time":"09:30:03","temp":"17.0", "humidity":"95.0" },
在 PHP 的帮助下,我阅读了它并像这样打印,效果很好:
<?php
$logFile = file_get_contents( "../../../home/shares/flower_hum/humid.log" );
echo $logFile;
?>
现在我想将其转换为 JSON 对象,但如果您注意到我缺少一些括号以使其有效。我需要删除最后一个逗号并添加一些括号,如下所示:
<?php
$logFile = file_get_contents( "../../../home/shares/flower_hum/humid.log" );
$stringLength = strlen($logFile)-2; //Get the length of the log
$logFile = substr($logFile, 0,$stringLength); //Removes the last comma.
$logFile = '{"log":[' . $logFile . ']}'; //Add brackets
echo $logFile; //Print result
$json = json_decode($logFile, true); //create JSON Object
?>
问题是每当我尝试将字符串添加到 $logFile 变量时,php 都会抛出一个错误(我不知道是哪个不幸的)。我可以连接 "normal" 字符串,例如 'Hello' 。 'World' 很好所以它必须用 get_file_contens 方法做一些事情。但我能找到的应该是 return 一个简单的字符串。
我想要的最终输出应该是这样的:
{"log":[
{"date":"15/09/2016", "time":"09:29:58","temp":"17.0","humidity":"95.0" },
{"date":"15/09/2016", "time":"09:30:01","temp":"17.0", "humidity":"95.0" },
{"date":"15/09/2016", "time":"09:30:03","temp":"17.0", "humidity":"95.0" }
]}
我可能会补充说我 运行 在我的 Raspberry Pi 上的 Apache 服务器 运行 上,但我已经安装了 PHP 并且有些东西确实有效,所以我不认为这与此有任何关系。
您可以借助 trim
删除换行符,rtrim
删除 array_map
回调中的尾随逗号来实现此目的。
还有一些来回 json_decode
和 json_encode
见下文
<?php
$logLines = file('logfile.txt');
$entries = array_map("clean",$logLines);
$finalOutput = [
'log' => $entries
];
print json_encode($finalOutput, JSON_UNESCAPED_SLASHES);
// add the flag so the slashes in the dates won't be escaped
function clean($string){
return json_decode(rtrim(trim($string),','),true);
}
这将输出
{"log":[{"date":"15/09/2016","time":"09:29:58","temp":"17.0","humidity":"95.0"},{"date":"15/09/2016","time":"09:30:01","temp":"17.0","humidity":"95.0"},{"date":"15/09/2016","time":"09:30:03","temp":"17.0","humidity":"95.0"}]}
所以我得到了一个打印以下日志的应用程序:
{"date":"15/09/2016", "time":"09:29:58","temp":"17.0", "humidity":"95.0" },
{"date":"15/09/2016", "time":"09:30:01","temp":"17.0", "humidity":"95.0" },
{"date":"15/09/2016", "time":"09:30:03","temp":"17.0", "humidity":"95.0" },
在 PHP 的帮助下,我阅读了它并像这样打印,效果很好:
<?php
$logFile = file_get_contents( "../../../home/shares/flower_hum/humid.log" );
echo $logFile;
?>
现在我想将其转换为 JSON 对象,但如果您注意到我缺少一些括号以使其有效。我需要删除最后一个逗号并添加一些括号,如下所示:
<?php
$logFile = file_get_contents( "../../../home/shares/flower_hum/humid.log" );
$stringLength = strlen($logFile)-2; //Get the length of the log
$logFile = substr($logFile, 0,$stringLength); //Removes the last comma.
$logFile = '{"log":[' . $logFile . ']}'; //Add brackets
echo $logFile; //Print result
$json = json_decode($logFile, true); //create JSON Object
?>
问题是每当我尝试将字符串添加到 $logFile 变量时,php 都会抛出一个错误(我不知道是哪个不幸的)。我可以连接 "normal" 字符串,例如 'Hello' 。 'World' 很好所以它必须用 get_file_contens 方法做一些事情。但我能找到的应该是 return 一个简单的字符串。
我想要的最终输出应该是这样的:
{"log":[
{"date":"15/09/2016", "time":"09:29:58","temp":"17.0","humidity":"95.0" },
{"date":"15/09/2016", "time":"09:30:01","temp":"17.0", "humidity":"95.0" },
{"date":"15/09/2016", "time":"09:30:03","temp":"17.0", "humidity":"95.0" }
]}
我可能会补充说我 运行 在我的 Raspberry Pi 上的 Apache 服务器 运行 上,但我已经安装了 PHP 并且有些东西确实有效,所以我不认为这与此有任何关系。
您可以借助 trim
删除换行符,rtrim
删除 array_map
回调中的尾随逗号来实现此目的。
还有一些来回 json_decode
和 json_encode
见下文
<?php
$logLines = file('logfile.txt');
$entries = array_map("clean",$logLines);
$finalOutput = [
'log' => $entries
];
print json_encode($finalOutput, JSON_UNESCAPED_SLASHES);
// add the flag so the slashes in the dates won't be escaped
function clean($string){
return json_decode(rtrim(trim($string),','),true);
}
这将输出
{"log":[{"date":"15/09/2016","time":"09:29:58","temp":"17.0","humidity":"95.0"},{"date":"15/09/2016","time":"09:30:01","temp":"17.0","humidity":"95.0"},{"date":"15/09/2016","time":"09:30:03","temp":"17.0","humidity":"95.0"}]}