PHP to write Tab Characters inside a file?

PhpStringCsvTsv

Php Problem Overview


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.

  • Real Tabs means, a true wide space we get when we press the <tab> key in a text-editor.

Php Solutions


Solution 1 - Php

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.

Solution 2 - Php

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

$chunk = "abc\tdef\tghi";

Solution 3 - Php

This should do:

$chunk = "abc\tdef\tghi";

Here is a link to an article with more extensive examples.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Question夏期劇場View Question on Stackoverflow
Solution 1 - PhpkapaView Answer on Stackoverflow
Solution 2 - PhpflowfreeView Answer on Stackoverflow
Solution 3 - PhpDrewnessView Answer on Stackoverflow