Windows 与 Linux PHP PHP_EOL - php 数据不同

问题描述

我将一个脚本从 Windows 迁移到了 rhel 8,并且我的 php 脚本中有以下代码:

$Data = file_get_contents('https://example.com/api/data'); 
$Array = json_decode($Data,true);
$Output = '';
foreach ($Array as $key => $value) {
 if ($Output != ''){$Output .= PHP_EOL;}
 $Output .= $value["id"] . ":" . $value["status"];
}
file_put_contents($Response,$Output);

在 Windows 中,它会在数据中添加一个漂亮的新行。在人力资源中不是。

我做了一个简单的测试:

$Output = '1';
$Output .= PHP_EOL;
$Output .= '2';
$Output .= PHP_EOL;
$Output .= '3';
file_put_contents($Response,$Output);

如果我执行十六进制转储,我会得到以下信息:

sh-4.4$ hexdump -c active.resp
0000000   1  \n   2  \n   3
0000005

sh-4.4$ hexdump active.resp
0000000 0a31 0a32 0033
0000005

带有 -c 的 hexdump 看起来是正确的。

如果我用 vi 创建同一个文件,十六进制转储会显示一个额外的 \n 和十六进制, 0a31 0a32 0a33

我在 $output 的末尾添加了一个额外的 PHP_EOL 并且它起作用了,并且 hexdump 显示的与 vi 创建的文件上的 hexdump 完全相同。

我不知所措。在 linux 上执行 file_put_contents 之前是否需要添加换行符?

有人可以解释一下这个和没有最后一个 php_eol 的 hexdump 吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)