开发者

Formatting a text file

I have this in my code

echo $result['arr']['sub']."\t\t\t\t".$result['arr2']['sub2'];

I am sending it through headers to a text file, but it shows up like this:

Subject              Description
Test                 Checking to see it it works
A long string             pushes my description to the side

How can I "fix" the formatting, so that the second column always lines up vertically, regardless of the length of text in the开发者_如何学编程 first column?


I think that you have misunderstood what tab spaces do. They don't magically consider text in the following lines; they just jump to the next free tab stop as configured by the display medium.

If the preceding text already went past a tab stop then, yes, it'll be skipped.

Perhaps you want a fixed-width margin after the first column.

Determine the number of spaces that you want to use (perhaps by taking the length of the longest item in the first column, and adding some padding width), then append that to each value in the first column with str_repeat, remembering to subtract the number of characters actually present in that field.


Perhaps like this, with a fixed first-column width of 20:

<?php
$column_width = 20;
foreach ($data as $result) {
   echo $result['arr']['sub'];
   echo str_repeat(' ', $column_width - strlen($result['arr']['sub']));
   echo $result['arr2']['sub2'];
   echo "\n";
}
?>

(I haven't accounted for the case where text in the first column exceeds a length of 20 characters.)


Or perhaps like this, with the clever expanding that I described up above:

<?php
$margin = 5;
$column_width = 0;
foreach ($data as $result) {
   $column_width = max($column_width, strlen($result['arr']['sub']));
}
$column_width += $margin;
foreach ($data as $result) {
   echo $result['arr']['sub'];
   echo str_repeat(' ', $column_width - strlen($result['arr']['sub']));
   echo $result['arr2']['sub2'];
   echo "\n";
}
?>

(Here the first column automatically expands, so it doesn't really matter how long it is.)


Live demo of both approaches.


Further work may include programtically limiting the length of text in the first column to something sensible for your output context, but I'll leave that up to you.


A improvement of this answer: Finds the best column width.

http://codepad.org/cXw8tXSr

$column_width = 2 + max(array_map(create_function('$x', 'return strlen($x["arr"]["sub"]);'), $data));
foreach ($data as $result) {
   echo $result['arr']['sub'];
   echo str_repeat(' ', $column_width - strlen($result['arr']['sub']));
   echo $result['arr2']['sub2'];
   echo "\n";
}


A simple solution using str_pad():

$data = array(
    'Subject'   =>  'Description',
    'Test'  =>  'Checking to see whether it works',
    'A really really really long string'    =>  'pushes my description to the side',
);

// Find the longest string for column 1
foreach ($data as $key => $val) {
    $width[] = strlen($key);
}

$column_width = max($width) + 5; // +5 for margin

foreach ($data as $key => $val) {
   echo str_pad($key, $column_width).$val."\n";
}

Demo: http://codepad.org/KoFxBis8

This will find the widest column and adjust accordingly. Just set the +5 to however many spaces you want between the columns.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜