夏期劇場
夏期劇場

Reputation: 18337

PHP to write Tab Characters inside a file?

How do i simply write out a file including real tabs inside? tab means real tab which is not the spaces. How to write the tab or what is the character for real tab?

For example here:

$chunk = "a,b,c";
file_put_contents("chunk.csv",$chunk);

What character should i use to get tabs instead of Commas (,) there?
In the output file, there should be real tabs between the seperated words.

Upvotes: 53

Views: 151982

Answers (3)

Drewness
Drewness

Reputation: 5072

This should do:

$chunk = "abc\tdef\tghi";

Upvotes: 7

kapa
kapa

Reputation: 78701

The tab character is \t. Notice the use of " instead of '.

$chunk = "<html>\t<head>\t\t<title>\t</head>";

PHP Strings - Double quoted

If the string is enclosed in double-quotes ("), PHP will interpret more escape sequences for special characters:

...

\t horizontal tab (HT or 0x09 (9) in ASCII)


Also, let me recommend the fputcsv() function which is for the purpose of writing CSV files.

Upvotes: 126

flowfree
flowfree

Reputation: 16462

Use \t and enclose the string with double-quotes:

$chunk = "abc\tdef\tghi";

Upvotes: 17

Related Questions