search

Home  >  Q&A  >  body text

How to write tab characters in files using PHP

How do I simply write out a file containing real tabs? tab represents the real tab, which is not a space. How to write tab or what characters are the real tab?

For example here:

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

What characters should I use to get tabs instead of comma (,) there?

In the output file, there should be real tabs between delimited words.


P粉399585024P粉399585024447 days ago667

reply all(2)I'll reply

  • P粉826283529

    P粉8262835292023-10-17 15:05:36

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

    $chunk = "abc\tdef\tghi";

    reply
    0
  • P粉121447292

    P粉1214472922023-10-17 14:01:25

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

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

    PHP String - Double Reference


    Also, let me recommend the fputcsv() function, which is used to write CSV files.

    reply
    0
  • Cancelreply