Jump to content

Php text file writing extra character


morrism35

Recommended Posts

My php file is writing an unwanted 0 at the end of the file. I plan on putting a tab in there to seperate the 
number and phrase, but that can come later also plan on using the $count variable later. Thank you.
 
Input:
jkhkjj
jkhlkhjljk
your word for the day
we wish you a merry christmas
good tightings to you
i love football
 
output:
0jkhkjj
0jkhlkhjljk
0your word for the day
0we wish you a merry christmas
0good tightings to you
0i love football
0
 
 
code:
$count=0;
$readin=file('proverb_load.txt');
foreach($readin as $r){
$num=0;
$handle = fopen("proverb_load.txt", 'a');
fwrite($handle, $num. $r);}
fclose($handle);
Link to comment
Share on other sites

your original text file probably contains an extra new-line/blank-line at the end.

 

you need to validate the data you are using to make sure the data is an expected value, type, format, ...

 

i would trim() the $r value, which will remove white-space characters before/after any data, and only write the line to the output file if the trimmed data is not an empty string. note: you have a new-line on the end of each real line that you probably don't want to remove or if you do remove it, you will want to add back to the output when you write the line to the output file.

Link to comment
Share on other sites

foreach($readin as $r){

$num=0;

$handle = fopen("proverb_load.txt", 'a');

trim($r);

fwrite($handle, $num.$r. PHP_EOL);}

fclose($handle);

 

The output is still the same even with trimming the $r variable. I'm not understanding the logic of what you are saying. Why is it writing every line correctly all but the last line where it is adding an extra 0, it looks like it's attempting to iterate one time to many but only outputting the $num variable?

Link to comment
Share on other sites

input:                                                                        

jkhkjj
jkhlkhjljk
your word for the day
we wish you a merry christmas
good tightings to you
i love football

 

 

output:

0jkhkjj
0
0jkhlkhjljk
0
0your word for the day
0
0we wish you a merry christmas
0
0good tightings to you
0
0i love football
0
0
0

 

Desiredoutput:                                                                        

0   jkhkjj   0
0   jkhlkhjljk   0
0   your word for the day      0
0   we wish you a merry christmas     0
0   good tightings to you     0

0   i love footballoutput     0

 

Starting to get really confused been on this assignment all day. I have other assignments were i've wrote text files like this I'm not

understanding why this is so diffucult. Please help

 

 

code:

$count=0;

$readin=file('proverb_load.txt', FILE_SKIP_EMPTY_LINES );
foreach($readin as $r){
$num=0;
$count=0;
$handle = fopen("proverb_load.txt", 'a');
fwrite($handle,$num.$r.$count.PHP_EOL);
}
fclose($handle);
Link to comment
Share on other sites

looks like its writing the $count variable on another line and the last 2 lines of extra zeros i don't know what thats all about.
 
$count=0;
$readin=file('proverb_load.txt', FILE_SKIP_EMPTY_LINES );
$handle = fopen("proverb_load.txt", 'a');
foreach($readin as $r){
$num=0;
$count=0;
 
fwrite($handle,$num.$r.$count);
}
fclose($handle);
 
 
output:
0jkhkjj
00jkhlkhjljk
00your word for the day
00we wish you a merry christmas
00good tightings to you
00i love football
00
0
Link to comment
Share on other sites

As I pointed out before, you input lines ($r) have eol at the end, so the second "0" will be at beginning of next line. Take out the newlines when you read.

$readin=file('proverb_load.txt', FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES );
$handle = fopen("proverb_load.txt", 'a');
foreach($readin as $r){
$num=0;
$count=0;
 
fwrite($handle,$num.$r.$count.PHP_EOL);
}
fclose($handle);

Input

jkhkjj
jkhlkhjljk
your word for the day
we wish you a merry christmas
good tightings to you
i love football

After processing

jkhkjj
jkhlkhjljk
your word for the day
we wish you a merry christmas
good tightings to you
i love football
0jkhkjj0
0jkhlkhjljk0
0your word for the day0
0we wish you a merry christmas0
0good tightings to you0
0i love football0

Edited by Barand
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.