note 60216 deleted from function.fopen by nlopess

From: Date: Fri, 30 Dec 2005 14:36:19 +0000
Subject: note 60216 deleted from function.fopen by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-100991@lists.php.net to get a copy of this message
Note Submitter: filter at stevenstromer dot com ---- While writing a function to export email addresses and user_ids from a db table to a csv text file on a Linux host, I was having a hard time getting line breaks to display properly when viewing the text file in TextEdit, BBEdit, TextWrangler and other text editors on the OS X host I was developing on. Looping through the query result array with the line: <? $recipients.= $get_addresses_row[0] . ',' . $get_addresses_row[1] . '\n'; ?> The resulting csv file loooked like: user1@domain1.com,1\nuser2@domain2.com,2\nuser3@domain3.com,3\n The exact same result occurred when swapping \r for \n, and even when swapping \r\n for \n. After completing the loop, I was opening and writing to the file with: <? $file_pointer = fopen($maillist_file, 'wb', 1); $file_written = fwrite($file_pointer, $recipients); ?> According to the PHP documentation, using \n in conjunction with the binary 'b' flag should have covered this situation. Finally, I revised the looping line to: <? $mass_recipient.= $get_addresses_row[0] . ',' . $get_addresses_row[1] . chr(13); ?> The chr(13) did the trick in this arrangement. The successful resulting csv file looks like: user1@domain1.com,1 user2@domain2.com,2 user3@domain3.com,3

« previous php.notes (#100991) next »