Display php-cli output in columns

Asked
Active3 hr before
Viewed126 times

8 Answers

display
90%

Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers ,You can pad $name to ensure a standard number of characters. Just ensure that the number of characters(20) is equal to or larger than the length of the longest name:,In example below 20 is the width of the first column. If you want to adjust alignment you can add a - or + to the column like "%-20s %s\n". There are many formatting options. See http://php.net/manual/en/function.sprintf.php, By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

You can pad $name to ensure a standard number of characters. Just ensure that the number of characters(20) is equal to or larger than the length of the longest name:

echo str_pad($name, 20).$work.
"\n";
load more v
88%

I want to output data with variable length in columns in a php-cli script,I would like to erase the cart content when the woocommerce session expires. I can see there's a variable setting the time in class WC_Session_Handler, however...,This is the #code# to detect the screen resolution on my page naming #code# and sending it to php, so that the values can be retrived using #code#:- This is the..., getting screen width into javascript variable and sending it through ajax to a php page to avoid page load This is the #code# to detect the screen resolution on my page naming #code# and sending it to php, so that the values can be retrived using #code#:- This is the... edmond98 · saved on 5 months ago

Example:

$pepole = Array(
   'Mirco Dellarovere' => 'Artista',
   'Nino Pepe' => 'Attore',
   'Zoe Yan' => 'Futurista',
   'Mino' => 'Elettricista'
);

foreach($pepole as $name => $work) {
   echo "$namet$workn";
}
load more v
72%

Clico also provides functionality for formatting tabular data for CLI output. The table class can also format the text in each row, each column, and each individual cell.,Once installed you can use the Text and Table classes in your own code., `getWidth()` - gets the number of columns in the table. , `setPipeCharacter(string $char)` - sets the character used to separate table columns.

Install via composer.

composer require webbtj / clico
load more v
65%

Outputting tables on the command line is fiddlier than it sounds, requiring some string padding trickery. Here’s a quick-and-dirty function to insta-win!,An interesting quirk of this function is that it’s more difficult to determine the width of each column than to actually output the table, and that’s where the difficulty is really: determining, for each column, the width of the longest piece of content and padding it to account for cell spacing.,When run in a command prompt, it’ll look something like this:,It’s not particularly clever though; it has no concept of headers (perhaps just provide the first row in uppercase) and doesn’t handle text wrapping, but for a quick-and-dirty table it does the job!

The Table function

function table($data) {

   // Find longest string in each column
   $columns = [];
   foreach($data as $row_key = & gt; $row) {
      foreach($row as $cell_key = & gt; $cell) {
         $length = strlen($cell);
         if (empty($columns[$cell_key]) || $columns[$cell_key] & lt; $length) {
            $columns[$cell_key] = $length;
         }
      }
   }

   // Output table, padding columns
   $table = '';
   foreach($data as $row_key = & gt; $row) {
      foreach($row as $cell_key = & gt; $cell)
      $table. = str_pad($cell, $columns[$cell_key]).
      '   ';
      $table. = PHP_EOL;
   }
   return $table;

}
load more v
75%

Search Answer Titles

$variable = "Hello";

var_dump($variable);
load more v
40%

system() - Execute an external program and display the output,passthru() - Execute an external program and display raw output,exec — Execute an external program, To get the output of the executed command, be sure to set and use the output parameter.

Returned with status 0 and output:
   Array(
      [0] => cmb
   )
22%

Replacing the line without moving the entire screen around is a much better technique. This can be done using the "\r" character. All you need to do is print out that character and whatever you print out next will replace the current line. The following will to the same as the previous example, but in this case only the last printed line will be replaced.,We can collect all of this together into a function that will replace all output that has previously been printed.,To use this function we need to pass an array of the bits of output we want to print. Here is an example of doing this in a loop to replace the output continuously.,We can use this technique to overwrite characters across multiple lines on the command line. To do with we need to count how many lines of content we have printed out and use the ANSI control sequence to move to the start of the line and move to the first line in the output.

The following will clear the console, print out "Please wait", wait 5 seconds and then replace that with "Done".

system('clear');
echo 'Please wait';
sleep(5);
system('clear');
echo 'Done';
load more v
60%

When building a console application it may be useful to display tabular data:,You can add a table separator anywhere in the output by passing an instance of TableSeparator as a row:,You can create a multiple-line page title using a header cell that spans the entire table width:,You can use the colspan and rowspan options at the same time which allows you to create any table layout you may wish.

1
2
3
4
5
6
7
8
+ -- -- -- -- -- -- -- - + -- -- -- -- -- -- -- -- -- -- -- -- -- + -- -- -- -- -- -- -- -- -- +
|
ISBN | Title | Author |
   + -- -- -- -- -- -- -- - + -- -- -- -- -- -- -- -- -- -- -- -- -- + -- -- -- -- -- -- -- -- -- +
   |
   99921 - 58 - 10 - 7 | Divine Comedy | Dante Alighieri |
   |
   9971 - 5 - 0210 - 0 | A Tale of Two Cities | Charles Dickens |
   |
   960 - 425 - 059 - 0 | The Lord of the Rings | J.R.R.Tolkien |
   |
   80 - 902734 - 1 - 6 | And Then There Were None | Agatha Christie |
   + -- -- -- -- -- -- -- - + -- -- -- -- -- -- -- -- -- -- -- -- -- + -- -- -- -- -- -- -- -- -- +
load more v

Other "display-undefined" queries related to "Display php-cli output in columns"