PHP to write Tab Characters inside a file?

偶尔善良 提交于 2019-12-30 00:13:31

问题


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.

回答1:


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

$chunk = "abc\tdef\tghi";

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.




回答2:


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

$chunk = "abc\tdef\tghi";



回答3:


This should do:

$chunk = "abc\tdef\tghi";

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



来源:https://stackoverflow.com/questions/11055153/php-to-write-tab-characters-inside-a-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!