mathiasppc Posted May 6, 2011 Share Posted May 6, 2011 Is this possible? Could the following code be replaced with one .txt consisting of two digits? $myFile = "offerrotation.txt"; $myFile2 = "lprotation.txt"; $fh = @fopen($myFile, 'r'); $offerNumber = @fread($fh, 5); @fclose($fh); $fh = @fopen($myFile2, 'r'); $lpNumber = @fread($fh, 5); @fclose($fh); Many thanks Quote Link to comment https://forums.phpfreaks.com/topic/235687-reading-and-writing-to-specific-parts-of-txt/ Share on other sites More sharing options...
Adam Posted May 6, 2011 Share Posted May 6, 2011 You'd be better having the digits one per line, then using file to read the contents of the file into an array: $rotation = file('rotation.txt'); $rotation[0] // contains first number $rotation[1] // contains second number Quote Link to comment https://forums.phpfreaks.com/topic/235687-reading-and-writing-to-specific-parts-of-txt/#findComment-1211402 Share on other sites More sharing options...
mathiasppc Posted May 6, 2011 Author Share Posted May 6, 2011 Thanks! How would you write them back to the file? Quote Link to comment https://forums.phpfreaks.com/topic/235687-reading-and-writing-to-specific-parts-of-txt/#findComment-1211410 Share on other sites More sharing options...
Adam Posted May 6, 2011 Share Posted May 6, 2011 If you write them both at the same time, you can just concatenate them within a string and use the new-line character ("\n") to put them on different lines: $new_rotation = $new_offer . "\n" . $new_lp; file_put_contents('rotation.txt', $new_rotation); Quote Link to comment https://forums.phpfreaks.com/topic/235687-reading-and-writing-to-specific-parts-of-txt/#findComment-1211426 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.