如何获得Linux控制台$ COLUMNS和$ ROWS从PHP CLI? [英] How to get linux console $COLUMNS and $ROWS from PHP cli?
问题描述
我目前正在创建一个新的整齐的CLI库PHP,我想弄清楚它在运行控制台的宽度/高度。
我试过很多东西想通过$ _ENV,EXEC挖掘(回声$列)等,但没有结果,而如果我输入echo $列或$ ROWS在bash命令行,它巧妙地显示值。
什么我需要做的,从PHP访问这个值?
我使用的脚本.SH这样的:
#!的/ usr / bin中/ PHP -q
< PHPrequire_once('lib.commandline.php');
一流的HelloWorld扩展CommandLineApp { 公共函数main(的$ args){ 回声(O海。'); }}
更新
最终的解决方案:
公共职能getScreenSize(){
preg_match_all(。/行([0-9] +);列([0-9] +); /,用strtolower(EXEC('的stty -a | grep的列')),$输出);
如果(sizeof的($输出)== 3){
$这个 - >设置['屏幕'] ['宽度'] = $输出[1] [0];
$这个 - >设置['屏幕'] ['高度'] = $输出[2] [0];
}
}
这不需要解析另一个shell选项是 tput的
:
$这个 - >设置['屏幕'] ['宽度'] = EXEC('tput的COLS)
$这个 - >设置['屏幕'] ['高度'] = EXEC('tput的行')
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];
}
}
Another shell option that requires no parsing is tput
:
$this->settings['screen']['width'] = exec('tput cols')
$this->settings['screen']['height'] = exec('tput lines')
这篇关于如何获得Linux控制台$ COLUMNS和$ ROWS从PHP CLI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!