user963631
user963631

Reputation: 195

Issue with control chars - PHP does not print \t (tab character)

i'm sending an XLS file with the header. Everything is working fine. But when i tell it to print the tab to separate the cells in the XLS by print \t. It does not prints the tab. It just prints the '\t' in the file and when i download the XLS file everything that should be in different cell is all in one cell with text like:

val1\tval2\tval3\t

Those three values should be separate in their separate cells. i have been trying for 2 hours now nothing is working :(.

i send headers like this:

header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=".$filename.$fileextention);
header("Pragma: no-cache");
header("Expires: 0");

and just print the values like

echo $val1 . '\t' . $val2 . '\t' . $val3;

i have tried using single or double quts. and print and echo both buy still :(

Upvotes: 2

Views: 1321

Answers (4)

Oswald
Oswald

Reputation: 31665

This should work:

echo $val1 . "\t" . $val2 . "\t" . $val3;

If it doesn't, it's a bug in PHP.

Upvotes: 0

Karoly Horvath
Karoly Horvath

Reputation: 96266

Use double quotes: "\t". Within single quotes only \\ and \' is recognised.

Upvotes: 1

Michal Szajter
Michal Szajter

Reputation: 94

Try this (see quotes)

header('Content-type: application/vnd.ms-excel');
echo "val1\tval2\tval3";

Upvotes: 2

maraspin
maraspin

Reputation: 2393

I'm quite sure that quotes cause the issue.

echo $val1 . "\t" . $val2 . "\t" . $val3;

should do the thing. Just look at this:

<?php

echo 'Test\tTest';
echo "\r\n";
echo "Test\tTest";
echo "\r\n";

which outputs:

Test\tTest
Test    Test

Use double quotes when you need control chars. Hopefully this can solve your problem.

Upvotes: 1

Related Questions