How to get linux console $COLUMNS and $ROWS from PHP cli? How to get linux console $COLUMNS and $ROWS from PHP cli? bash bash

How to get linux console $COLUMNS and $ROWS from PHP cli?


Another shell option that requires no parsing is tput:

$this->settings['screen']['width'] = exec('tput cols')$this->settings['screen']['height'] = exec('tput lines')


Use the PHP ncurses_getmaxyx function.

ncurses_getmaxyx (STDSCR, $Height, $Width)

PREVIOUSLY:

http://php.net/manual/en/function.getenv.php

$cols = getenv('COLUMNS');$rows = getenv('ROWS');

The "proper" way is probably to call the TIOCGSIZE ioctl to get the kernel's idea of the window size, or call the command stty -a and parse the results for rows and columns


$COLUMNS and $LINES is probably not being exported to your program. You can run export LINES COLUMNS before running your app, or you can get this information directly:

$fp=popen("resize", "r");$b=stream_get_contents($fp);preg_match("/COLUMNS=([0-9]+)/", $b, $matches);$columns = $matches[1];preg_match("/LINES=([0-9]+)/", $b, $matches);$rows = $matches[1];pclose($fp);