hrishi
hrishi

Reputation: 1656

PHP - While writing a fixed length file, PHP is not processing `\t` as Tab after the first one. Why?

I am writing a fixed length file using PHP. Each character will have a predefined length. If characters are less than the length then it will insert that number of blank spaces. Each field is separated by TAB.

$str1 = str_pad('ten',10)."\t";
$str2 = str_pad('seven',7)."\t";
$str3 = str_pad('fifteen',15)."\t";

$str = $str1.$str2.$str3;

file_put_contents("newfile.txt",$str);

But TAB is not working as expected.

I can see TAB only after first word (ten). After that there are no more TABs in the output file.

Upvotes: 1

Views: 262

Answers (3)

Rafael Beckel
Rafael Beckel

Reputation: 2524

Your text is rendered correctly.

A tab does not mean a fixed number of spaces or a printable character. It means "forward the cursor to the next tab stop".

A tab stop in computers is typically set to every 4 or 8 characters. It's a display issue that will behave differently across various systems and user configurations.

If you copy your output to your preferred text editor and manually select it, you can see spaces as dots and tabs as lines, as in the examples below:

Note: your output is the first line ("ten seven fifteen"). I added the second and third lines to illustrate the configured tab length in the text editor.

If we set tab length to 4: Tab stops set to 4

The first string contains 10 chars ("t", "e", "n" plus 7 dots), and the next tab stop is at the 12th char. So it's only 2 chars away, that's why your tab character (grey line) is only 2 chars long. The second string has 7 chars, and the next stop is only 1 char away so the tab will have only 1 char. It's the same logic for the third string.

If we set tab length to 8: Tab stops set to 8

It follows the same logic, but in this example, the first tab is now bigger because the nearest stop is at position 16, so the tab has 6 chars. The second and third tabs have coincidentally only one char.

If we set tab length to an odd number like 3: Tab stops set to 3

This is not common, but it's something possible. In this example, we can see all tabs as multiple spaces. It follows the same logic as explained above, but we can visually see that your code is producing the expected output.

Notice that I did not change your output in any way. I just played with the editor's configuration.

If you want a fixed length spacer between your chars, you should use "\s" instead. You can repeat the character like this: "\s\s\s\s", or using str_repeat("\s", 4); or "\s" * 4 if you were using Python.

Upvotes: 2

helpdoc
helpdoc

Reputation: 1990

In Your Case, try this code :

$str1 = str_pad('ten',10)."\t";
$str2 = str_pad('seven',7)."\t\t";
$str3 = str_pad('fifteen',15)."\t\t";

$str = $str1.$str2.$str3;

file_put_contents("newfile.txt",$str);

Upvotes: 0

davi_singh
davi_singh

Reputation: 160

It looks like when you are adding the padding to the string it is considering the TAB character as a space. I would suggest you change the code to the following to get the desired effect.

$str1 = str_pad('ten',10);
$str2 = str_pad('seven',7);
$str3 = str_pad('fifteen',15);

$str = $str1."\t".$str2."\t".$str3."\t";

file_put_contents("newfile.txt",$str);

If there are a huge number of strings I would suggest putting them in an array and doing a foreach loop appending the tab character at the end.

By the way there is no tab character in your final string all you are seeing is the padding of spaces after the strings

Upvotes: 0

Related Questions