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:
(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:
(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.