Welcome! Log In Create A New Profile

Advanced

[PHP] CSV Files

Posted by Jason Todd Slack-Moehrle 
Jason Todd Slack-Moehrle
[PHP] CSV Files
October 23, 2008 02:05AM
Hi All,

I need to take a record in MySQL and have my user get it as a CSV
File. I am OK with most everything, except when I create the CSV file,
I write out the column headers then I need to write out the data.

After I right out the column headers do I have to put a '/n' to have
it start a new line in the CSV file? I think I do.

Here is what I have (a snippet):

header("Content-type: application/csv");
header($filenameString);
header("Pragma: no-cache");
header("Expires: 0");

echo $map_BORROWER_FIRST_NAME.", ";
echo $map_BORROWER_LAST_NAME.", ";
echo $map_BORROWER_SSN.", ";
echo $map_BORROWER_HOME_PHONE.", ";
echo $map_BORROWER_DOB;
echo "/n";
echo $row[BORROWER_FIRST_NAME].",";
echo $row[BORROWER_LAST_NAME].",";
echo $row[BORROWER_SSN].",";
echo $row[BORROWER_HOME_PHONE_FIRST].$row[BORROWER_HOME_PHONE_MIDDLE].
$row[BORROWER_HOME_PHONE_LAST].",";
echo $row[BORROWER_DOB_MONTH].$row[BORROWER_DOB_DAY].
$row[BORROWER_DOB_YEAR];

When I run this, I see my column names, but not the data. The query
seems to execute just fine....

Any advice?

Thanks,

-Jason



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Eric Butera
Re: [PHP] CSV Files
October 23, 2008 02:10AM
On Wed, Oct 22, 2008 at 7:59 PM, Jason Todd Slack-Moehrle
<[email protected]> wrote:
> Hi All,
>
> I need to take a record in MySQL and have my user get it as a CSV File. I am
> OK with most everything, except when I create the CSV file, I write out the
> column headers then I need to write out the data.
>
> After I right out the column headers do I have to put a '/n' to have it
> start a new line in the CSV file? I think I do.
>
> Here is what I have (a snippet):
>
> header("Content-type: application/csv");
> header($filenameString);
> header("Pragma: no-cache");
> header("Expires: 0");
>
> echo $map_BORROWER_FIRST_NAME.", ";
> echo $map_BORROWER_LAST_NAME.", ";
> echo $map_BORROWER_SSN.", ";
> echo $map_BORROWER_HOME_PHONE.", ";
> echo $map_BORROWER_DOB;
> echo "/n";
> echo $row[BORROWER_FIRST_NAME].",";
> echo $row[BORROWER_LAST_NAME].",";
> echo $row[BORROWER_SSN].",";
> echo
> $row[BORROWER_HOME_PHONE_FIRST].$row[BORROWER_HOME_PHONE_MIDDLE].$row[BORROWER_HOME_PHONE_LAST].",";
> echo
> $row[BORROWER_DOB_MONTH].$row[BORROWER_DOB_DAY].$row[BORROWER_DOB_YEAR];
>
> When I run this, I see my column names, but not the data. The query seems to
> execute just fine....
>
> Any advice?
>
> Thanks,
>
> -Jason
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

When I work with things like this I always use fputcsv. One neat
little trick is to use streams.

$o = fopen("php://output", "w");

// select first row and use array keys for header row then rewind

// while results do this
fputcsv($o, $row)

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Stut
Re: [PHP] CSV Files
October 23, 2008 02:15AM
On 23 Oct 2008, at 00:59, Jason Todd Slack-Moehrle wrote:
> After I right out the column headers do I have to put a '/n' to have
> it start a new line in the CSV file? I think I do.

A new line is \n not /n, and it must be in double quotes (") not
single (').

-Stut

--
http://stut.net/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jason Todd Slack-Moehrle
Re: [PHP] CSV Files
October 23, 2008 03:45AM
Hi Stut,

Actually i am ending the row headers with a chr(10); // LINE FEED

-Jason

On Oct 22, 2008, at 5:12 PM, Stut wrote:

> On 23 Oct 2008, at 00:59, Jason Todd Slack-Moehrle wrote:
>> After I right out the column headers do I have to put a '/n' to
>> have it start a new line in the CSV file? I think I do.
>
> A new line is \n not /n, and it must be in double quotes (") not
> single (').
>
> -Stut
>
> --
> http://stut.net/
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Stut
Re: [PHP] CSV Files
October 23, 2008 04:05AM
On 23 Oct 2008, at 02:41, Jason Todd Slack-Moehrle wrote:
> Actually i am ending the row headers with a chr(10); // LINE FEED

From the code you included in your original post...

> echo "/n";


There was no mention of chr(10).

Outputting data in CSV format is not hard. Simply echo the header row
if necessary, followed by "\n". Then output each line taking care to
put string values in quotes which means you also need to escape quotes
in the data. After each line echo "\n". That's really all there is to
it.

If you're still having problems I suggest you post the exact code
you're using, anything else just makes it harder for us to provide
effective help.

-Stut

--
http://stut.net/

> On Oct 22, 2008, at 5:12 PM, Stut wrote:
>
>> On 23 Oct 2008, at 00:59, Jason Todd Slack-Moehrle wrote:
>>> After I right out the column headers do I have to put a '/n' to
>>> have it start a new line in the CSV file? I think I do.
>>
>> A new line is \n not /n, and it must be in double quotes (") not
>> single (').

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jason Todd Slack-Moehrle
Re: [PHP] CSV Files
October 23, 2008 04:20AM
Oh, I am not putting quotes around each field that i get from MySQL.
There are no quotes in the data so that is good.

Sorry I put "/n" and I meant to put chr(10).

-Jason

On Oct 22, 2008, at 6:58 PM, Stut wrote:

> On 23 Oct 2008, at 02:41, Jason Todd Slack-Moehrle wrote:
>> Actually i am ending the row headers with a chr(10); // LINE FEED
>
> From the code you included in your original post...
>
>> echo "/n";
>
>
> There was no mention of chr(10).
>
> Outputting data in CSV format is not hard. Simply echo the header
> row if necessary, followed by "\n". Then output each line taking
> care to put string values in quotes which means you also need to
> escape quotes in the data. After each line echo "\n". That's really
> all there is to it.
>
> If you're still having problems I suggest you post the exact code
> you're using, anything else just makes it harder for us to provide
> effective help.
>
> -Stut
>
> --
> http://stut.net/
>
>> On Oct 22, 2008, at 5:12 PM, Stut wrote:
>>
>>> On 23 Oct 2008, at 00:59, Jason Todd Slack-Moehrle wrote:
>>>> After I right out the column headers do I have to put a '/n' to
>>>> have it start a new line in the CSV file? I think I do.
>>>
>>> A new line is \n not /n, and it must be in double quotes (") not
>>> single (').
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Robert Cummings
Re: [PHP] CSV Files
October 23, 2008 04:30AM
On Thu, 2008-10-23 at 02:58 +0100, Stut wrote:
> On 23 Oct 2008, at 02:41, Jason Todd Slack-Moehrle wrote:
> > Actually i am ending the row headers with a chr(10); // LINE FEED
>
> From the code you included in your original post...
>
> > echo "/n";
>
>
> There was no mention of chr(10).
>
> Outputting data in CSV format is not hard. Simply echo the header row
> if necessary, followed by "\n". Then output each line taking care to
> put string values in quotes which means you also need to escape quotes
> in the data. After each line echo "\n". That's really all there is to
> it.
>
> If you're still having problems I suggest you post the exact code
> you're using, anything else just makes it harder for us to provide
> effective help.

Almost... except as usual Microsoft made something extremely simply bite
you in the ass:

http://support.microsoft.com/default.aspx?scid=kb;en-us;323626&Product=xlw

It actually bit me in the ass several years ago.

Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Andrew Ballard
Re: [PHP] CSV Files
October 23, 2008 04:35AM
On Wed, Oct 22, 2008 at 10:15 PM, Jason Todd Slack-Moehrle
<[email protected]> wrote:
> On Oct 22, 2008, at 6:58 PM, Stut wrote:
>
>> On 23 Oct 2008, at 02:41, Jason Todd Slack-Moehrle wrote:
>>>
>>> Actually i am ending the row headers with a chr(10); // LINE FEED
>>
>> From the code you included in your original post...
>>
>>> echo "/n";
>>
>>
>> There was no mention of chr(10).
>>
>> Outputting data in CSV format is not hard. Simply echo the header row if necessary, followed by "\n". Then output each line taking care to put string values in quotes which means you also need to escape quotes in the data. After each line echo "\n". That's really all there is to it.
>>
>> If you're still having problems I suggest you post the exact code you're using, anything else just makes it harder for us to provide effective help.
>>
>> -Stut
>>
>>> On Oct 22, 2008, at 5:12 PM, Stut wrote:
>>>
>>>> On 23 Oct 2008, at 00:59, Jason Todd Slack-Moehrle wrote:
>>>>>
>>>>> After I right out the column headers do I have to put a '/n' to have it start a new line in the CSV file? I think I do.
>>>>
>>>> A new line is \n not /n, and it must be in double quotes (") not single (').
>>
>
> Oh, I am not putting quotes around each field that i get from MySQL. There are no quotes in the data so that is good.
>
> Sorry I put "/n" and I meant to put chr(10).
>
> -Jason
>

Jason, one of the points that Stut was trying to explain is that "\n"
and chr(10) are the same thing. They are just two different ways to
refer to a newline (line feed) character. Most of us probably use "\n"
rather than chr(10) in PHP, though. So, the following two lines are
equivalent:

<?php
echo "Item1, Item2, Item3" . chr(10);

// Note this uses double quotes.
echo "Item1, Item2, Item3\n";

// It won't be the same at all if you use single quotes
echo 'Item1, Item2, Item3\n';

?>

At any rate, you are correct that you need a line feed/newline
character at the end of every row in CSV including the header row.

Andrew

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Ashley Sheridan
Re: [PHP] CSV Files
October 23, 2008 08:20AM
On Wed, 2008-10-22 at 22:30 -0400, Andrew Ballard wrote:
> On Wed, Oct 22, 2008 at 10:15 PM, Jason Todd Slack-Moehrle
> <[email protected]> wrote:
> > On Oct 22, 2008, at 6:58 PM, Stut wrote:
> >
> >> On 23 Oct 2008, at 02:41, Jason Todd Slack-Moehrle wrote:
> >>>
> >>> Actually i am ending the row headers with a chr(10); // LINE FEED
> >>
> >> From the code you included in your original post...
> >>
> >>> echo "/n";
> >>
> >>
> >> There was no mention of chr(10).
> >>
> >> Outputting data in CSV format is not hard. Simply echo the header row if necessary, followed by "\n". Then output each line taking care to put string values in quotes which means you also need to escape quotes in the data. After each line echo "\n". That's really all there is to it.
> >>
> >> If you're still having problems I suggest you post the exact code you're using, anything else just makes it harder for us to provide effective help.
> >>
> >> -Stut
> >>
> >>> On Oct 22, 2008, at 5:12 PM, Stut wrote:
> >>>
> >>>> On 23 Oct 2008, at 00:59, Jason Todd Slack-Moehrle wrote:
> >>>>>
> >>>>> After I right out the column headers do I have to put a '/n' to have it start a new line in the CSV file? I think I do.
> >>>>
> >>>> A new line is \n not /n, and it must be in double quotes (") not single (').
> >>
> >
> > Oh, I am not putting quotes around each field that i get from MySQL. There are no quotes in the data so that is good.
> >
> > Sorry I put "/n" and I meant to put chr(10).
> >
> > -Jason
> >
>
> Jason, one of the points that Stut was trying to explain is that "\n"
> and chr(10) are the same thing. They are just two different ways to
> refer to a newline (line feed) character. Most of us probably use "\n"
> rather than chr(10) in PHP, though. So, the following two lines are
> equivalent:
>
> <?php
> echo "Item1, Item2, Item3" . chr(10);
>
> // Note this uses double quotes.
> echo "Item1, Item2, Item3\n";
>
> // It won't be the same at all if you use single quotes
> echo 'Item1, Item2, Item3\n';
>
> ?>
>
> At any rate, you are correct that you need a line feed/newline
> character at the end of every row in CSV including the header row.
>
> Andrew
>
A line feed and \n are not the same thing at all. Different operating
systems implemented a different method of line endings depending on what
they thought best, either a carriage return or line feed or both. a \n
is meant to be an OS agnostic way of implementing this in the various
programming languages, outputting something that any OS can understand
in the intended way.


Ash
www.ashleysheridan.co.uk


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Andrew Ballard
Re: [PHP] CSV Files
October 23, 2008 08:30AM
On Thu, Oct 23, 2008 at 2:22 AM, Ashley Sheridan
<[email protected]> wrote:
> On Wed, 2008-10-22 at 22:30 -0400, Andrew Ballard wrote:
>> On Wed, Oct 22, 2008 at 10:15 PM, Jason Todd Slack-Moehrle
>> <[email protected]> wrote:
>> > On Oct 22, 2008, at 6:58 PM, Stut wrote:
>> >
>> >> On 23 Oct 2008, at 02:41, Jason Todd Slack-Moehrle wrote:
>> >>>
>> >>> Actually i am ending the row headers with a chr(10); // LINE FEED
>> >>
>> >> From the code you included in your original post...
>> >>
>> >>> echo "/n";
>> >>
>> >>
>> >> There was no mention of chr(10).
>> >>
>> >> Outputting data in CSV format is not hard. Simply echo the header row if necessary, followed by "\n". Then output each line taking care to put string values in quotes which means you also need to escape quotes in the data. After each line echo "\n". That's really all there is to it.
>> >>
>> >> If you're still having problems I suggest you post the exact code you're using, anything else just makes it harder for us to provide effective help.
>> >>
>> >> -Stut
>> >>
>> >>> On Oct 22, 2008, at 5:12 PM, Stut wrote:
>> >>>
>> >>>> On 23 Oct 2008, at 00:59, Jason Todd Slack-Moehrle wrote:
>> >>>>>
>> >>>>> After I right out the column headers do I have to put a '/n' to have it start a new line in the CSV file? I think I do.
>> >>>>
>> >>>> A new line is \n not /n, and it must be in double quotes (") not single (').
>> >>
>> >
>> > Oh, I am not putting quotes around each field that i get from MySQL. There are no quotes in the data so that is good.
>> >
>> > Sorry I put "/n" and I meant to put chr(10).
>> >
>> > -Jason
>> >
>>
>> Jason, one of the points that Stut was trying to explain is that "\n"
>> and chr(10) are the same thing. They are just two different ways to
>> refer to a newline (line feed) character. Most of us probably use "\n"
>> rather than chr(10) in PHP, though. So, the following two lines are
>> equivalent:
>>
>> <?php
>> echo "Item1, Item2, Item3" . chr(10);
>>
>> // Note this uses double quotes.
>> echo "Item1, Item2, Item3\n";
>>
>> // It won't be the same at all if you use single quotes
>> echo 'Item1, Item2, Item3\n';
>>
>> ?>
>>
>> At any rate, you are correct that you need a line feed/newline
>> character at the end of every row in CSV including the header row.
>>
>> Andrew
>>
> A line feed and \n are not the same thing at all. Different operating
> systems implemented a different method of line endings depending on what
> they thought best, either a carriage return or line feed or both. a \n
> is meant to be an OS agnostic way of implementing this in the various
> programming languages, outputting something that any OS can understand
> in the intended way.
>
>
> Ash
> www.ashleysheridan.co.uk

As I understood, \n was strictly a line feed (ASCII character 10), not
an "OS agnostic" end-of-line terminator. It happens to be the line
terminator for *nix. Windows uses the combined carriage return and
line feed characters (ASCII characters 13 and 10) which are
represented by \r\n in PHP, while Mac used only the the carriage
return. There is a PHP constant PHP_EOL that I'm pretty sure is
supposed to represent the line terminator defined on the operating
system of the computer executing the script, but I don't think it is
truly "agnostic" either. At least, if you have a text file saved in
Windows and split it on a Linux machine based on PHP_EOL, I believe
all of your array values will have a carriage return character hanging
on the end of them. Am I mistaken?

Andrew

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Ashley Sheridan
Re: [PHP] CSV Files
October 23, 2008 08:50AM
On Thu, 2008-10-23 at 02:26 -0400, Andrew Ballard wrote:
> On Thu, Oct 23, 2008 at 2:22 AM, Ashley Sheridan
> <[email protected]> wrote:
> > On Wed, 2008-10-22 at 22:30 -0400, Andrew Ballard wrote:
> >> On Wed, Oct 22, 2008 at 10:15 PM, Jason Todd Slack-Moehrle
> >> <[email protected]> wrote:
> >> > On Oct 22, 2008, at 6:58 PM, Stut wrote:
> >> >
> >> >> On 23 Oct 2008, at 02:41, Jason Todd Slack-Moehrle wrote:
> >> >>>
> >> >>> Actually i am ending the row headers with a chr(10); // LINE FEED
> >> >>
> >> >> From the code you included in your original post...
> >> >>
> >> >>> echo "/n";
> >> >>
> >> >>
> >> >> There was no mention of chr(10).
> >> >>
> >> >> Outputting data in CSV format is not hard. Simply echo the header row if necessary, followed by "\n". Then output each line taking care to put string values in quotes which means you also need to escape quotes in the data. After each line echo "\n". That's really all there is to it.
> >> >>
> >> >> If you're still having problems I suggest you post the exact code you're using, anything else just makes it harder for us to provide effective help.
> >> >>
> >> >> -Stut
> >> >>
> >> >>> On Oct 22, 2008, at 5:12 PM, Stut wrote:
> >> >>>
> >> >>>> On 23 Oct 2008, at 00:59, Jason Todd Slack-Moehrle wrote:
> >> >>>>>
> >> >>>>> After I right out the column headers do I have to put a '/n' to have it start a new line in the CSV file? I think I do.
> >> >>>>
> >> >>>> A new line is \n not /n, and it must be in double quotes (") not single (').
> >> >>
> >> >
> >> > Oh, I am not putting quotes around each field that i get from MySQL. There are no quotes in the data so that is good.
> >> >
> >> > Sorry I put "/n" and I meant to put chr(10).
> >> >
> >> > -Jason
> >> >
> >>
> >> Jason, one of the points that Stut was trying to explain is that "\n"
> >> and chr(10) are the same thing. They are just two different ways to
> >> refer to a newline (line feed) character. Most of us probably use "\n"
> >> rather than chr(10) in PHP, though. So, the following two lines are
> >> equivalent:
> >>
> >> <?php
> >> echo "Item1, Item2, Item3" . chr(10);
> >>
> >> // Note this uses double quotes.
> >> echo "Item1, Item2, Item3\n";
> >>
> >> // It won't be the same at all if you use single quotes
> >> echo 'Item1, Item2, Item3\n';
> >>
> >> ?>
> >>
> >> At any rate, you are correct that you need a line feed/newline
> >> character at the end of every row in CSV including the header row.
> >>
> >> Andrew
> >>
> > A line feed and \n are not the same thing at all. Different operating
> > systems implemented a different method of line endings depending on what
> > they thought best, either a carriage return or line feed or both. a \n
> > is meant to be an OS agnostic way of implementing this in the various
> > programming languages, outputting something that any OS can understand
> > in the intended way.
> >
> >
> > Ash
> > www.ashleysheridan.co.uk
>
> As I understood, \n was strictly a line feed (ASCII character 10), not
> an "OS agnostic" end-of-line terminator. It happens to be the line
> terminator for *nix. Windows uses the combined carriage return and
> line feed characters (ASCII characters 13 and 10) which are
> represented by \r\n in PHP, while Mac used only the the carriage
> return. There is a PHP constant PHP_EOL that I'm pretty sure is
> supposed to represent the line terminator defined on the operating
> system of the computer executing the script, but I don't think it is
> truly "agnostic" either. At least, if you have a text file saved in
> Windows and split it on a Linux machine based on PHP_EOL, I believe
> all of your array values will have a carriage return character hanging
> on the end of them. Am I mistaken?
>
> Andrew
I'm afraid I do disagree with your there:

"When writing a file in text mode, '\n' is transparently translated to
the native newline sequence used by the system"

This is from the Wikipedia article I found:
http://en.wikipedia.org/wiki/Newline



Ash
www.ashleysheridan.co.uk


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Andrew Ballard
Re: [PHP] CSV Files
October 23, 2008 06:40PM
On Thu, Oct 23, 2008 at 2:54 AM, Ashley Sheridan
<[email protected]> wrote:
> On Thu, 2008-10-23 at 02:26 -0400, Andrew Ballard wrote:
>> On Thu, Oct 23, 2008 at 2:22 AM, Ashley Sheridan
>> <[email protected]> wrote:
>> > On Wed, 2008-10-22 at 22:30 -0400, Andrew Ballard wrote:
>> >> On Wed, Oct 22, 2008 at 10:15 PM, Jason Todd Slack-Moehrle
>> >> <[email protected]> wrote:
>> >> > On Oct 22, 2008, at 6:58 PM, Stut wrote:
>> >> >
>> >> >> On 23 Oct 2008, at 02:41, Jason Todd Slack-Moehrle wrote:
>> >> >>>
>> >> >>> Actually i am ending the row headers with a chr(10); // LINE FEED
>> >> >>
>> >> >> From the code you included in your original post...
>> >> >>
>> >> >>> echo "/n";
>> >> >>
>> >> >>
>> >> >> There was no mention of chr(10).
>> >> >>
>> >> >> Outputting data in CSV format is not hard. Simply echo the header row if necessary, followed by "\n". Then output each line taking care to put string values in quotes which means you also need to escape quotes in the data. After each line echo "\n". That's really all there is to it.
>> >> >>
>> >> >> If you're still having problems I suggest you post the exact code you're using, anything else just makes it harder for us to provide effective help.
>> >> >>
>> >> >> -Stut
>> >> >>
>> >> >>> On Oct 22, 2008, at 5:12 PM, Stut wrote:
>> >> >>>
>> >> >>>> On 23 Oct 2008, at 00:59, Jason Todd Slack-Moehrle wrote:
>> >> >>>>>
>> >> >>>>> After I right out the column headers do I have to put a '/n' to have it start a new line in the CSV file? I think I do.
>> >> >>>>
>> >> >>>> A new line is \n not /n, and it must be in double quotes (") not single (').
>> >> >>
>> >> >
>> >> > Oh, I am not putting quotes around each field that i get from MySQL. There are no quotes in the data so that is good.
>> >> >
>> >> > Sorry I put "/n" and I meant to put chr(10).
>> >> >
>> >> > -Jason
>> >> >
>> >>
>> >> Jason, one of the points that Stut was trying to explain is that "\n"
>> >> and chr(10) are the same thing. They are just two different ways to
>> >> refer to a newline (line feed) character. Most of us probably use "\n"
>> >> rather than chr(10) in PHP, though. So, the following two lines are
>> >> equivalent:
>> >>
>> >> <?php
>> >> echo "Item1, Item2, Item3" . chr(10);
>> >>
>> >> // Note this uses double quotes.
>> >> echo "Item1, Item2, Item3\n";
>> >>
>> >> // It won't be the same at all if you use single quotes
>> >> echo 'Item1, Item2, Item3\n';
>> >>
>> >> ?>
>> >>
>> >> At any rate, you are correct that you need a line feed/newline
>> >> character at the end of every row in CSV including the header row.
>> >>
>> >> Andrew
>> >>
>> > A line feed and \n are not the same thing at all. Different operating
>> > systems implemented a different method of line endings depending on what
>> > they thought best, either a carriage return or line feed or both. a \n
>> > is meant to be an OS agnostic way of implementing this in the various
>> > programming languages, outputting something that any OS can understand
>> > in the intended way.
>> >
>> >
>> > Ash
>> > www.ashleysheridan.co.uk
>>
>> As I understood, \n was strictly a line feed (ASCII character 10), not
>> an "OS agnostic" end-of-line terminator. It happens to be the line
>> terminator for *nix. Windows uses the combined carriage return and
>> line feed characters (ASCII characters 13 and 10) which are
>> represented by \r\n in PHP, while Mac used only the the carriage
>> return. There is a PHP constant PHP_EOL that I'm pretty sure is
>> supposed to represent the line terminator defined on the operating
>> system of the computer executing the script, but I don't think it is
>> truly "agnostic" either. At least, if you have a text file saved in
>> Windows and split it on a Linux machine based on PHP_EOL, I believe
>> all of your array values will have a carriage return character hanging
>> on the end of them. Am I mistaken?
>>
>> Andrew
> I'm afraid I do disagree with your there:
>
> "When writing a file in text mode, '\n' is transparently translated to
> the native newline sequence used by the system"
>
> This is from the Wikipedia article I found:
> http://en.wikipedia.org/wiki/Newline
>
>
>
> Ash
> www.ashleysheridan.co.uk
>
>

Interesting. I didn't know it behaved differently when writing files.
I just know that when using it as a search pattern or for splitting
text that it is only the newline character. I thought the behavior you
are describing was specifically why PHP added PHP_EOL. Further down
the same article it says:

"Some languages have created special variables, constants and
subroutines to facilitate newlines during program execution. One
example is the PHP constant PHP_EOL, which will produce either '\r\n'
or '\n' appropriate to the operating system the program is executed
on.[3] Though special newline handling facilities can aid execution
during runtime, they do not ensure the validity of newlines for the
source code itself."

I guess since PHP is written in C it picks up the behavior you
describe from the underlying libraries. That's definitely interesting
to keep in mind.

Andrew

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Sorry, only registered users may post in this forum.

Click here to login