【问题标题】:Printing better formating in command line在命令行中打印更好的格式
【发布时间】:2011-04-01 18:53:44
【问题描述】:

我有一个通过命令行运行的 php 脚本,例如

php file.php

在那个文件中我有一个类似的打印语句

print "<br>Saved the url: {$url} to :{$destination}";

我认为 br 会在另一个下方将其共振峰 1,但是当我运行脚本时,我得到了这种很难阅读的格式

<br>Saved the url: http://example.com/a.mp3 to :/usr/recordings/3e/1555141317-2349577.mp3<br>Saved the url: http://example.com/b.mp3 to :/usr/recordings

所以格式很难在控制台中阅读。有没有办法重组我的打印以得到这样的输出

Saved the url: http://example.com/a.mp3 to :/usr/recordings/3e/1555141317-dadfdasffa.mp3
Saved the url: http://example.com/b.mp3 to :/usr/recordings/3c/1555141317-fddfd.mp3
Saved the url: http://example.com/c.mp3 to :/usr/recordings/3f/1555141317-ffdfd.mp3

【问题讨论】:

    标签: terminal php


    【解决方案1】:

    使用换行符代替 br。

    print "\nSaved the url: {$url} to :{$destination}";
    

    如果您也希望它与 html 输出一起使用,您可以检查您正在运行的 sapi:

    echo PHP_SAPI == 'cli' ? PHP_EOL : '<br>', "Saved the url: {$url} to :{$destination}";
    

    【讨论】:

    • 如果您不介意输出中的 BR,您始终可以同时使用这两种方法-&lt;br/&gt;\n
    【解决方案2】:

    你试过 \n\r 或 \n 吗?
    用于 html。控制台不同。

    【讨论】:

      【解决方案3】:
      print "Saved the url: {$url} to :{$destination}\n";
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-12
        • 2013-10-09
        • 2011-09-11
        • 1970-01-01
        • 1970-01-01
        • 2018-01-16
        相关资源
        最近更新 更多