doudun3910 2010-02-04 21:39
浏览 47
已采纳

如何从PHP cli获取linux控制台$ COLUMNS和$ ROWS?

I'm currently creating a new neat CLI library for PHP, and i'd like to figure out the width/height of the console it's running in.

I've tried many things like digging through $_ENV, exec("echo $COLUMNS"), etc, but no result, while if i type echo $COLUMNS or $ROWS in bash commandline, it neatly displays the value.

What do i need to do to access this value from PHP?

I'm using .sh scripts like this:

#!/usr/bin/php -q
<?php

require_once('lib.commandline.php');


class HelloWorld extends CommandLineApp {

  public function main($args) {

       echo('O, Hai.');

    }

}

Update Final solution:

public function getScreenSize() { 
      preg_match_all("/rows.([0-9]+);.columns.([0-9]+);/", strtolower(exec('stty -a |grep columns')), $output);
      if(sizeof($output) == 3) {
        $this->settings['screen']['width'] = $output[1][0];
        $this->settings['screen']['height'] = $output[2][0];
      }
    }
  • 写回答

6条回答 默认 最新

  • dongzhuang5741 2010-02-05 00:12
    关注

    Another shell option that requires no parsing is tput:

    $this->settings['screen']['width'] = exec('tput cols')
    $this->settings['screen']['height'] = exec('tput lines')
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch