我们正在获取 .txt 文件,但没有得到正确的对齐

发布于 2024-09-01 17:32:14 字数 729 浏览 1 评论 0原文

当我们将数据导出到 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

舂唻埖巳落 2024-09-08 17:32:14

您需要使用制表符(\t)而不是空格来获得类似列的对齐方式

这是更新的示例

  $myFile = "user_password.txt";
  $fh = fopen($myFile, 'a') or die("can't open file");
  $newline ="\r\n";
  fwrite ($fh,$newline);

  $stringData1 = $_POST['uname1']."\t" ;
  fwrite($fh, $stringData1);

  $stringData1 =$_POST['password1']."\t";
  fwrite($fh,$stringData1);

  $stringData1 = $_POST['email1']."\t";
  fwrite($fh, $stringData1);

  fclose($fh);

You need to use tabs (\t) instead of spaces to get column-like alignement

Here is your example updated

  $myFile = "user_password.txt";
  $fh = fopen($myFile, 'a') or die("can't open file");
  $newline ="\r\n";
  fwrite ($fh,$newline);

  $stringData1 = $_POST['uname1']."\t" ;
  fwrite($fh, $stringData1);

  $stringData1 =$_POST['password1']."\t";
  fwrite($fh,$stringData1);

  $stringData1 = $_POST['email1']."\t";
  fwrite($fh, $stringData1);

  fclose($fh);
笑咖 2024-09-08 17:32:14

您需要打印一个 "\t" (制表符)而不是三个连接的空格。

像这样

$stringData1 = $_POST['uname1']."\t";
fwrite($fh, $stringData1);

编辑:如果字符串长度的差异超过一个选项卡,这可能并不总是有效。如果您要创建制表符分隔文件,这将起作用。如果您想要某种列视图,这不是一个好方法..(另一个编辑;paxdiablos 的答案对于第二种方法更好。)

You need to print a "\t" (tab) instead of three concatenated spaces.

Like so

$stringData1 = $_POST['uname1']."\t";
fwrite($fh, $stringData1);

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.)

梦幻的味道 2024-09-08 17:32:14

您似乎所做的就是在每个字段的末尾添加三个空格。也许你应该做的事情更像是:

$stringData1 = substr($_POST['uname1'] . "              ",0,14) . " ";
fwrite($fh, $stringData1);

换句话说,确保字段有 14 个字符或更多,将其截断为 14。

或者更好的是,使用 printf 设施:

fprintf ($fh, "%-14s ", $_POST['uname1']);

事实上,你的整个段可以压缩为类似的内容:

$myFile = "user_password.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
fprintf ($fh, "\r\n%14s %14s %14s",  $_POST['uname1'], $_POST['password1'],
    $_POST['email1']);
fclose($fh);

All you seem to be doing is appending three spaces to the end of each field. Perhaps you should be doing something more like:

$stringData1 = substr($_POST['uname1'] . "              ",0,14) . " ";
fwrite($fh, $stringData1);

In other words, make sure the field is 14 characters or more, the truncate it to 14.

Or better yet, use the printf facilities:

fprintf ($fh, "%-14s ", $_POST['uname1']);

In fact, your entire segment could be compressed to something like:

$myFile = "user_password.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
fprintf ($fh, "\r\n%14s %14s %14s",  $_POST['uname1'], $_POST['password1'],
    $_POST['email1']);
fclose($fh);
飘然心甜 2024-09-08 17:32:14

好吧,您正在将 uname1、password1 和 email1 写入一个文件,并用三个空格分隔(并且出于某种原因,在每行末尾多了三个空格)。

使用 str_repeat 添加所需数量的空格: http://php.net/manual/de/function.str-repeat.php

像这样:

$stringData1 = $POST['uname1'] . str_repeat(" ", $longest_uname - strlen($POST['uname1']) + 1);
fwrite($fh, $stringData1);
$stringData1 = $POST['password1'] . str_repeat(" ", $longest_password - strlen($POST['password1']) + 1);
fwrite($fh, $stringData1);
$stringData1 = $POST['email1'] . str_repeat(" ", $longest_email - strlen($POST['email1']) + 1);
fwrite($fh, $stringData1);

您需要首先通过迭代数组并找到 $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.php

Like this:

$stringData1 = $POST['uname1'] . str_repeat(" ", $longest_uname - strlen($POST['uname1']) + 1);
fwrite($fh, $stringData1);
$stringData1 = $POST['password1'] . str_repeat(" ", $longest_password - strlen($POST['password1']) + 1);
fwrite($fh, $stringData1);
$stringData1 = $POST['email1'] . str_repeat(" ", $longest_email - strlen($POST['email1']) + 1);
fwrite($fh, $stringData1);

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 ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文