note 60216 added to function.fopen
| From: | filter at stevenstromer dot com | Date: | Fri, 30 Dec 2005 01:58:29 +0000 |
| Subject: | note 60216 added to function.fopen | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-100971@lists.php.net to get a copy of this message | ||
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
----
Manual Page -- http://www.php.net/manual/en/function.fopen.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+60216
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+60216&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+60216&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+60216&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+60216&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+60216&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+60216&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+60216&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+60216&report=yes
Search -- http://master.php.net/manage/user-notes.php