2017-10-30 15:00
浏览 75


PHP. fputs(STDOUT, 'some short text')! Is there any way to get char length for terminal that launch php script?

How to print text with fputs(STDOUT, "Some Error "); with full line background of red color?

图片转代码服务由CSDN问答提供 功能建议

PHP。 fputs(STDOUT,'some short text')! 有没有办法获得启动php脚本的终端的char长度?

如何使用 fputs(STDOUT,“Some Error ”); 打印带有红色全线背景的文本? < / DIV>

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dtf76989 2017-10-30 16:09

    This will output a line with "=" characters filling the whole line of the terminal. Will work on linux-based system.

    // 'tput cols' is a shell command which returns
    //   the number of chars to fill up the whole
    //   line of the current terminal.
    $colCount = shell_exec('tput cols');
    // Create the string filling the whole line
    $string = str_repeat("=", intval($colCount));
    // This is the way to print out text with coloured background
    // 48, 5, 160 representing 256-colour of red, green and blue.
    fputs(STDOUT, "\e[48;5;160m".$string."\e[0m");

    Which can be condensed to one line

    fputs(STDOUT, "\e[48;5;160m".str_repeat("=", intval(shell_exec('tput cols')))."\e[0m");

    Here's a script to ANSI color code in XTerm/ANSI-compatible terminals with a 256-color palette support if you want to know more about displaying colours on terminal.

    打赏 评论
  • dongshou9878 2017-10-30 18:09

    You can use Symfony\Console for this, since it has Terminal class that does exactly what you need:

    Install with composer

    composer require symfony/console

    Create script index.php

    require __DIR__ . '/vendor/autoload.php';
    $terminal = new Symfony\Component\Console\Terminal;
    $terminalWidth = $terminal->getWidth();

    Test it

    $ php index.php
    打赏 评论

相关推荐 更多相似问题