Daevid Vincent wrote:
I write many CLI scripts in PHP to clean up DB records, etc via crontab
scripts. Often though, I implement command line switches to 'debug' or see
various data as a precaution before actually purging.
Perl has some neat tools built in to format tabular data:
http://www.usenix.org/publications/perl/perl08.html
Does PHP have any tools like this? I'd prefer not to pull in some PEAR
package or other bloat, but I'll take what I can get if it actually works
well. I was hoping there are some tips/tricks to show simple tabular data:
i.e.:
# foo.php --showgroups
==================================
Groups
==================================
Name: Expires: Date:
-------- ------------ -----------
groupA 3 hours 2007-10-08
groupBeta 10 hours 2007-11-10
groupC 1 week 2007-12-31
notice basic things like alignment,
length of data (as in 'tabs' won't work), etc.
Try this out
<plaintext><?php
$data[] = array('one', 'two', 'three');
$data[] = array('oneoneone', 'two', 'three');
$data[] = array('oneone', 'two', 'threeone');
$data[] = array('one', 'onetwo', 'three');
$data[] = array('one', 'two', 'oneoneonethree');
$data[] = array('one', 'two', 'three');
function displayFixedWidth($data) {
$seperation = 3;
$seperator = '=';
$columnWidths = array();
foreach($data AS $header=>$row) {
foreach($row AS $column => $value) {
$columnWidths[$column] = max(strlen($value),
@$columnWidths[$column]);
}
}
$totalWidth = 0;
foreach($columnWidths AS $length) {
$totalWidth += ($length+$seperation);
}
echo str_pad('', ($totalWidth+count($columnWidths)), $seperator)."\n";
foreach($columnWidths AS $header => $length) {
echo ' '.str_pad($header, ($length+$seperation), ' ');
}
echo "\n".str_pad('', ($totalWidth+count($columnWidths)),
$seperator)."\n";
foreach($data AS $header=>$row) {
foreach($row AS $column => $value) {
echo ' '.str_pad($value, ($columnWidths[$column]+$seperation));
}
echo "\n";
}
echo str_pad('', ($totalWidth+count($columnWidths)), $seperator)."\n";
}
displayFixedWidth($data);
--
Jim Lucas
"Perseverance is not a long race;
it is many short races one after the other"
Walter Elliot
"Some men are born to greatness, some achieve greatness,
and some have greatness thrust upon them."
Twelfth Night, Act II, Scene V
by William Shakespeare
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php