morrism35 Posted November 19, 2015 Share Posted November 19, 2015 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: jkhkjjjkhlkhjljkyour word for the daywe wish you a merry christmasgood tightings to youi love football output:0jkhkjj0jkhlkhjljk0your word for the day0we wish you a merry christmas0good tightings to you0i love football0 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); Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted November 19, 2015 Share Posted November 19, 2015 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. Quote Link to comment Share on other sites More sharing options...
morrism35 Posted November 19, 2015 Author Share Posted November 19, 2015 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? Quote Link to comment Share on other sites More sharing options...
morrism35 Posted November 19, 2015 Author Share Posted November 19, 2015 i get it now it is reading an extra line or space. I removed the $num variable and it still wrote a 0. I thought the zero was from the $num variable but apparently its just a space. Now i guess I need to write some time of "if" statement to check for an non-empty line. Quote Link to comment Share on other sites More sharing options...
Barand Posted November 19, 2015 Share Posted November 19, 2015 try using $readin=file('proverb_load.txt', FILE_SKIP_EMPTY_LINES ); Quote Link to comment Share on other sites More sharing options...
morrism35 Posted November 19, 2015 Author Share Posted November 19, 2015 input: jkhkjjjkhlkhjljkyour word for the daywe wish you a merry christmasgood tightings to youi love football output: 0jkhkjj00jkhlkhjljk00your word for the day00we wish you a merry christmas00good tightings to you00i love football000 Desiredoutput: 0 jkhkjj 00 jkhlkhjljk 00 your word for the day 00 we wish you a merry christmas 00 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); Quote Link to comment Share on other sites More sharing options...
Barand Posted November 19, 2015 Share Posted November 19, 2015 The lines in your input already have EOL at the end. You are adding an extra when you write. Also, open the output file before the loop, write records in the loop, close file after the loop Quote Link to comment Share on other sites More sharing options...
morrism35 Posted November 19, 2015 Author Share Posted November 19, 2015 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: 0jkhkjj00jkhlkhjljk00your word for the day00we wish you a merry christmas00good tightings to you00i love football000 Quote Link to comment Share on other sites More sharing options...
Barand Posted November 19, 2015 Share Posted November 19, 2015 (edited) 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 November 19, 2015 by Barand Quote Link to comment Share on other sites More sharing options...
morrism35 Posted November 19, 2015 Author Share Posted November 19, 2015 Thank you that worked. This command wasn't explained in my manual at least not that i had seen. FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES Quote Link to comment Share on other sites More sharing options...
Barand Posted November 19, 2015 Share Posted November 19, 2015 http://php.net/manual/en/function.file.php Parameter section flags subsection Quote Link to comment Share on other sites More sharing options...
morrism35 Posted November 19, 2015 Author Share Posted November 19, 2015 This site seems to have everything you need to know about php. I've been here before and some of the stuff here looks really advanced. Thank you Quote Link to comment Share on other sites More sharing options...
Barand Posted November 19, 2015 Share Posted November 19, 2015 This site seems to have everything you need to know about php. It should have. That's THE PHP MANUAL. What have you been reading? Quote Link to comment Share on other sites More sharing options...
morrism35 Posted November 20, 2015 Author Share Posted November 20, 2015 my classroom text book php mysql programming by Don Gosselin. The reviews say it is one of the worst books for learning php. I've only been learning php about 3 months since the start of fall semester. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.