我经常在命令行上执行以下操作:
<?php
echo 'Short description:' . "\t\t\t\t\t\t" . '123' . "\n";
echo 'A slightly longer description:' . "\t\t\t\t\t\t" . '321' . "\n";
?>
这些将不再“匹配”; “ 123”和“ 321”将不在同一“列”上:
是否有任何明智的方法来解决此问题?我一直以为“制表人”的全部目的就是要达到这种效果,但是显然不是吗?还是我以某种方式误解了?
答案 0 :(得分:0)
这是一种方法。
也许不像您期望的那么简单,但是可以。
我使用完整的字符串并找到最长的描述,并将其用作基础。
$str = "Short description: \t 123
A slightly longer description: \t 321";
$arr = explode ("\n", $str);
foreach($arr as $line){
// Create new array with str_len as one item
$parts = explode("\t", $line);
$new[] = ['desc' => $parts[0], 'num' => $parts[1], 'len' => strlen($parts[0])];
}
// Find max length in array
$pad = max(array_column($new, 'len'))+1;
// Output it
foreach($new as $sub){
echo str_pad($sub['desc'], $pad, " ") . "\t" . $sub['num'] . "\n";
}
输出:
Short description: 123
A slightly longer description: 321