【问题标题】:What is changing my line breaks?是什么改变了我的换行符?
【发布时间】:2015-01-18 02:39:30
【问题描述】:

我正在尝试从 Windows 上的 PHP 命令行工具读取一些二进制数据。最初的工具是 git show,但我也可以用 type 重现问题(以确保没有发生 Git autocrlf)。

我不确定发生了什么,但似乎在转换行尾的方式的某个地方。观察...

在命令行上:

# dir temp
18.01.2015  03:24             1.362 temp

在 PHP 中:

$data = `type d:\\temp`;
echo strlen($data);                            // outputs "1302"
echo strlen(str_replace("\n", "\r\n", $data)); // outputs "1362"
echo strlen(file_get_contents('d:\\temp'));    // outputs "1362"

发生了什么事?如何获取逐字数据?

【问题讨论】:

    标签: php windows shell line-endings


    【解决方案1】:

    file_get_contents() 将为您提供逐字数据,二进制安全。避免使用特定于操作系统的内容,例如您使用的 type

    尝试使用其他方法而不是反引号,例如 passthru

    ob_start();
    passthru("git command blah blah");
    $var = ob_get_clean();
    echo strlen($var);
    

    【讨论】:

    • 没有文件。这是我想要的 git show 的输出。
    • @AndreKR,你应该在一开始就直接告诉这个。我已经更新了答案。
    【解决方案2】:

    我发现的解决方法是这样的:

    ob_start();
    passthru('type d:\\temp');
    $data = ob_get_clean();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-24
      • 2011-11-12
      相关资源
      最近更新 更多