Using PHP to extract data from SQL and then creating a .csv file on the server for emailing/download by way of fputcsv. All works well other than trying to get a new line within a field in Excel (2003).
I get Product1Product2Product3 in the cell when I need
Product1
Product2
Product3
I have tried single quotes, double quotes, CRs, LFs and I am rapidly disapearing up my own backside.
So the question is - what character do I need to get into the CSV file to achieve this?
It has to work in Excel
[Solution] - the problem lay somewhere in passing the \r\n to Excel through the PHP fputcsv - I was unable to achieve this in any sort of fashion (plenty of appending the desired \r\n to my actual cell data e.g. Product1\r\n)
The suggestion to use $lfcr = chr(10) . chr(13); worked first time around. I guess this was more of a PHP rather than an Excel question - thanks to all resonses.
This is more of an excel rather than a php question. What you put into the csv file needs to be understood by excel which is why \r\n will not work.
Use this
$lfcr = chr(10) . chr(13);
Then append $lfcr to the end of each line.
for linebreaks in a field in a csv-file you just have to surround the field with double quotes and prepend a = like in the following example. the linebreaks itself can be either \r\n or just \n:
id;product;price
1;iMac;2.99
2;="product
with
linebreaks";1.99
3;Bananaphone;999.99
Related
I know that for any of the languages for example php, if I am exporting data into csv file and if data contains the delimiter such as ; or , I need to wrap the data with ""
For example if my data is Hello, World after csv is exported Hello and World will not be in the same column and instead different. By solving this issue I know lots posts are saying wrap with "which will then be "Hello, World" then the output in csv will be ""Hello, World" which is within the same row and column BUT the qoutes " will also be in the csv file
Having this image as example:
row 2 is pretty much what is needed BUT it has qoutes too after exporting. My question is, is there is way to get ride of the qoutes BUT also keep the delimeter such as comma or semicolon in place?
Thanks in advance for any help / advices
For some reason with my code which I'm using \n to break the line, isn't working.
It is outputting without breaking the lines.
$file = "availables.txt";
$current .= $test."\n";
file_put_contents($file, $current);
I'm not sure what's wrong but I read that using single quotes (') doesn't work and I have to use double quotes (") which is what I'm using.
I couldn't really find much but I'm sure this is just a simple issue :)
As alluded to in the comments the issue is in selecting the correct line ending symbol for the platform.
The core constant PHP_EOL will do this for you allowing you to create portable code.
The correct 'End Of Line' symbol for this platform. Available since PHP 4.3.10 and PHP 5.0.2
It's true that a line feed character enclosed in single quotes won't work, that's simply because PHP will insert a backspace \ followed by an n into the file. This results in the ASCII/UTF-8 hex code 0x2F0x6E instead of simply 0xA.
Using either "\n" or the predefined constant PHP_EOL which will always contain the correct line feed combination for your platform will resolve this issue.
There's absolutely nothing wrong with the code you posted in your question. So what's wrong? My guess is the program you use to view the resulting file. Some broken Windows applications won't read the line feed correctly because they expect the file to use Windows line feeds (which is a combination of line feed and carriage return as you might know).
You're using PHP, output the file in your browser (no matter which), they'll handle it correctly. Simply:
<?php
header("content-type: text/plain");
echo "hello\nworld";
And you'll see that everything is fine.
You can write you variable inside double quotes like this
$current .= "$test\n";
I'm generating a CSV file using PHP and opening it in Excel. Adding "\n" at the end of each record creates a new line and works perfectly, however I need to figure out a way to create a newline within a cell itself. The same functionality that alt+enter achieves when entering data manually into Excel. Does anyone have any insight as to how this can be performed? I have tried "\n\r", "\n", chr(10), none of which seem to work, just keep getting a complete new line instead of newline within the same cell.
What I want to achieve is a header that looks like this...
This is all in one row in Excel..
CELL
Start Date
End Date
Thank you for any help provided!
How are you creating the csv file?
If you're doing it correctly and using fputcsv, having a line break within a cell wouldn't cause an issue.
Documentation to fputcsv
I have tried "\n\r", "\n",
Use "\r\n" :) Check the wiki article about 'Newline'
But note that you'll have to use double quotes. " otherwise PHP will not handle meta characters.
I have exported data to a text file using PHP/MYSQL. I wrote into file.txt created using the instruction below:
fwrite($fp ,'' . $value. '' . "\t");
Every thing goes right, but some problem appears when a field in the DB contains a ',' character, like this:
Section= Society, Education & Youth
So in the text file created the section value appears in two columns separated and that's wrong, because the value of section is a one and should be inserted in one cell (I see the problem in excel file)
So the problem is, how can I tell the output to ignore the ',' in some values so that it wouldn't be taken as two columns?
A comma-separated value (CSV) file can use a delimiter character, usually quotes, to denote text for just that case. If your data does not have quotes within the text than you can give that a try. You can tell Excel what the delimiter is. You can also use a different characters (tab, comma, etc.) to delimit the fields just as long as Excel knows how you're delimiting the data.
// try with quotes
fwrite($fp, "\"$value\"\t");
I have a CSV parser, that takes Outlook 2010 Contact Export .CSV file, and produces an array of values.
I break each row on the new line symbol, and each column on the comma. It works fine, until someone puts a new line inside a field (typically Address). This new line, which I assume is "\n" or "\r\n", explodes the row where it shouldn't, and the whole file becomes messed up from there on.
In my case, it happens when Business Street is written in two lines:
123 Apple Dr. Unit A
My code:
$file = file_get_contents("outlook.csv");
$rows = explode("\r\n",$file);
foreach($rows as $row)
{
$columns = explode(",",$row);
// Further manipulation here.
}
I have tried both "\n" and "\r\n", same result.
I figured I could calculate the number of columns in the first row (keys), and then find a way to not allow a new line until this many columns have been parsed, but it feels shady.
Is there another character for the new line that I can try, that would not be inside the data fields themselves?
The most common way of handling newlines in CSV files is to "quote" fields which contain significant characters such as newlines or commas. It may be worth looking into whether your CSV generator does this.
I recommend using PHP's fgetcsv() function, which is intended for this purpose. As you've discovered, splitting strings on commas works only in the most trivial cases.
In cases, where that doesn't work, a more sophisticated, reportedly RFC4180-compliant parser is available here.
I also recommend fgetcsv()
fgetcsv will also take care of commas inside strings ( between quotes ).
Interesting parsing tutorial
+1 to the previous answer ;)
PS: fgetcsv is a bit slower then opening the file and explode the contents etc. But imo it's worth it.