我们正在获取 .txt 文件,但没有得到正确的对齐
当我们将数据导出到 .txt 文件时,我们得到以下 texfile_screenshot1.JPG
我们需要 texfile_screenshot2.JPG 中显示的输出
以下是代码
$myFile = "user_password.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$newline ="\r\n";
fwrite ($fh,$newline);
$stringData1 = $_POST['uname1']." "." "." " ;
fwrite($fh, $stringData1);
$stringData1 =$_POST['password1']." "." "." ";
fwrite($fh,$stringData1);
$stringData1 = $_POST['email1']." "." "." ";
fwrite($fh, $stringData1);
fclose($fh);
we are getting the following texfile_screenshot1.JPG when we are exporting data to .txt file
we need output which is shown in texfile_screenshot2.JPG
following is the code
$myFile = "user_password.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$newline ="\r\n";
fwrite ($fh,$newline);
$stringData1 = $_POST['uname1']." "." "." " ;
fwrite($fh, $stringData1);
$stringData1 =$_POST['password1']." "." "." ";
fwrite($fh,$stringData1);
$stringData1 = $_POST['email1']." "." "." ";
fwrite($fh, $stringData1);
fclose($fh);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要使用制表符(\t)而不是空格来获得类似列的对齐方式
这是更新的示例
You need to use tabs (\t) instead of spaces to get column-like alignement
Here is your example updated
您需要打印一个
"\t"
(制表符)而不是三个连接的空格。像这样
编辑:如果字符串长度的差异超过一个选项卡,这可能并不总是有效。如果您要创建制表符分隔文件,这将起作用。如果您想要某种列视图,这不是一个好方法..(另一个编辑;paxdiablos 的答案对于第二种方法更好。)
You need to print a
"\t"
(tab) instead of three concatenated spaces.Like so
EDIT: This may not always work though, if your the difference in length of the strings are more than one tab. If you're creating a tab delimited file, this will work. If you want some sort of column-view this is not a good approach.. (another edit; paxdiablos answer is better for the second method.)
您似乎所做的就是在每个字段的末尾添加三个空格。也许你应该做的事情更像是:
换句话说,确保字段有 14 个字符或更多,将其截断为 14。
或者更好的是,使用 printf 设施:
事实上,你的整个段可以压缩为类似的内容:
All you seem to be doing is appending three spaces to the end of each field. Perhaps you should be doing something more like:
In other words, make sure the field is 14 characters or more, the truncate it to 14.
Or better yet, use the printf facilities:
In fact, your entire segment could be compressed to something like:
好吧,您正在将 uname1、password1 和 email1 写入一个文件,并用三个空格分隔(并且出于某种原因,在每行末尾多了三个空格)。
使用
str_repeat
添加所需数量的空格: http://php.net/manual/de/function.str-repeat.php像这样:
您需要首先通过迭代数组并找到 $longest_uname、$longest_password、$longest_email 来找出 $longest_uname、$longest_password、$longest_email每列的最长字符串。
如果您不需要最后一列右侧填充空格,则可以跳过“longest_email”部分。
编辑:当然,这里提到的“制表符”解决方案也可以工作,但前提是一列中字符串的长度之间的差异不超过一个制表符。 “substr(..., 14)”方法也可以工作,但前提是没有字符串超过 14 个字符...
Well, you're writing uname1, password1 and email1 to a file, separated with three spaces (and for what reason ever, three more spaces at the end of each line).
Use
str_repeat
to add as many spaces as you need: http://php.net/manual/de/function.str-repeat.phpLike this:
You'll need to find out $longest_uname, $longest_password, $longest_email first by iterating through your array and finding the longest string for each of your columns.
If you don't need the last column to be right-padded with spaces, you can skip the "longest_email"-part.
EDIT: Of course the "tab"-solutions mentioned here will work, too, but only if the difference between the lengths of your strings in one column will not exceed one tab. Also the "substr(..., 14)"-method will work, but only if no string is longer than 14 characters ...