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 th开发者_如何学Goe 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);
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);
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.)
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);
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 ...
精彩评论