Export to txt file error occurred - php

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");

Related

is there a way to remove qoutes " in csv but also include semicolon after exporting?

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

How to format a large text field in php?

I have one column named description of mysql text data type. So when retrive the value from this column I need to show it in diffrent text columns. how to divide this single large text into text columns. any one have idea how to do this.
use the word wrap function
http://php.net/manual/en/function.wordwrap.php
example wordwrap($str, 80);
for a new line separator every 80 chars, and not splitting words
Try using a delimiter, example " : ", this makes formats it to be used as a serialized string, when you retrieve it from the database just explode by this delimiter

line break within data for Excel 2003

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

Line break issue from CSV to MySQL

I am importing a .csv file into MySQL and everything works fine, except the line breaks that are in the file.
One of my .csv rows looks like this:
42,E-A-R™ Classic™ Earplugs,ear,images/ear/classic.jpg,5%,"Proven size, shape, and foam
3M's most popular earplug
Corded and uncorded in a variety of individual packs
NRR 29 dB / CSA Class AL",312-1201,,"E-A-R™ Classic™ Uncorded Earplugs, in Poly Bag",310-1001,,E-A-R™ Classic™ Uncorded Earplugs in Pillow Pack,311-1101,,"E-A-R™ Classic™ Corded Earplugs, in Poly Bag"
The sixth field over should break into a new line when called, but it doesn't. When importing the .csv I select Lines terminated by \r. I have tried \n and auto but no luck.
Weird thing is, the field looks correct in the database with all of the appropriate breaks. If I manually go in to insert the line breaks in PHPmyadmin it prints correctly. Each field is set to UTF-8 as well.
Any ideas on this? Thanks.
edit: here is the MySQL statement
LOAD DATA LOCAL INFILE '/tmp/php89FC0F' REPLACE INTO TABLE `ohes_flyer_products`
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\r'
LOAD DATA LOCAL INFILE '/tmp/php89FC0F' REPLACE INTO TABLE `ohes_flyer_products`
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\r\n'
maybe you could use fgetcsv to parse each csv line into an array and then dump that array into the database?
something along the lines of
$fd = fopen($csvfile, "r");
while ($line = fgetcsv($fd))
{
$sql = sprintf("INSERT INTO tablename (...) VALUES ('%s', ...)", $line[0], ...);
$res = mysql_query($sql);
}
note 1: code not ready for production, check SQL injections!
note 2: please, use prepared statements as using them will speed the thing a lot (or make one multi-row insert statement).
note 3: wrap all in a transaction.
Your CSV file has some qualities that you might be able to exploit.
The field containing carriage returns that do not terminate the record are enclosed in quotation marks.
The carriage return denoting the end of record follows a record with data enclosed in quotation marks. If this is true for all records, it is a way to possibly distinguish mid-field carriage returns from record terminators.
Knowing this, here are some things you can try:
Using a program like UltraEdit (or Notepad++) and its find/replace features (that include regular expression handling):
Find all carriage returns that are preceded by a quotation mark and replace them with a unique character or string. I suggest the pipe character "|" but first ensure they aren't used anywhere in the CSV file. These will represent end-of-record.
Next, replace all carriage returns with spaces. This will bring your fields with unwanted carriage returns back into alignment with the other data.
Finally, replace all special end-of-record characters with carriage returns. The end result that the only carriage returns present are end-of-record indicators.
Given that the carriage returns appear within a field that is enclosed by a delimiter (the quotation marks) you can specify that the import engine should only honor field and record delimiters outside of quotations. (MySQL LOAD DATA INFILE syntax) Specifically, look at the ENCLOSED BY 'char' parameter. Since not all of your fields use the delimiter, you will need to specify OPTIONALLY. In theory you should be able to specify how the CSV file is constructed and not need to parse it beforehand. I am of the opinion, however, that the in-field carriage returns should probably be removed so that the text will properly wrap when output in new context.
Your CSV appears to be non-standard, but that's often the reality of dealing with customer datasets.
As tools like MySQL's LOAD DATA statement are made to handle only the perfect use case, I've found that dealing with non-standard datasets like this requires code.
One way to handle this is to first scrub your CSV, replacing mid-field line breaks with a special, unique string (like ===MIDFIELD_LINE_BREAK===). Then I would write a custom CSV parser in a scripting language (Python, Ruby, PHP, Perl, etc).
In your CSV parser, iterate through lines in the file. For each line:
Swap the \n or \r characters back in for the ===MIDFIELD_LINE_BREAK=== characters.
Construct and execute an INSERT statement.
This worked for me:
$query = <<<EOT
LOAD DATA LOCAL INFILE '$file' REPLACE INTO TABLE `$table`
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '\\\'
LINES TERMINATED BY '\\\n'
IGNORE 1 ROWS;
EOT;
I had to tweak #Krunal's answer, due to getting errors, by adding a few extra forward slashes.
Unix line returns used here, by the way.
DOS: \\\r\\\n
Old Mac: \\\r
Unix: \\\n

Creating carriage returns in csv cell via php

I'm trying to dynamically generate a csv file with some cells that will contain multiple lines, the address field for example will need to be grouped into a single "address" cell instead of address,city,state etc. All is going well and but for the last two days i've tried to insert \r, \r\n, \n, chr(10),chr(13), as well as a carriage return within the code to create the carriage return i'm looking for within the cell. All of these fail, either being literally printed in my csv as "\r" etc or when I do a manual carriage return in the code it generates a new row. I'm using this to create the breaks in my cells but it isn't working
$groupedCell = implode('\r',$data);
I'm pretty sure the code is correct as its placing \r where I would like a carriage return but not the actual return i'm looking for. I've tried some different encodings but still no luck, I am testing in Open Office which I guess could be the issue but I would assume it can handle carriage returns within a cell and I haven't seen any documentation to support otherwise. Thanks for reading!
The CSV spec is one I find implemented in many different ways... it basically seems like it's only half-speced which is frustrating given it's popularity.
To include a new-line within a cell in a CSV there cell may need to be wrapped, or the new-line may need to be escaped. You'll notice from the linked doc there are three ways to do this - and different programmes treat it differently:
Excel wraps the whole cell in double quotes: a cell can have (unescaped) newline characters within it and be considered a single cell, as long as it's wrapped in double quotes (note also you'll need to use excel-style double quote escaping within the cell contents)
Other programmes insert a single backslash before the character, therefore a line ending in \ is not considered the end of a line, but a newline character within the cell. A cell can have unescaped newline characters within as long as they're preceded by the backslash character.
Others still replace a newline with C-style character escaping, the actual character sequence \n or \r\n. In this case the cell has fully escaped newline characters.
The problem is compounded by the potential need to escape the control characters (as well as other content (eg " in #1, and \ in #2+3) and different styles of escaping (eg. an embedded quote could be escaped as: double double quote "" or backslash-double quote \")
My advice: generate an open-office document with multiple lines and key escape characters and see how open-office generates a CSV file. From there you can decide which of the above methods to use for newlines within cells, and which escaping method.
example of style-1 (excel):
#num,str,num
1,"Hello
World",1990
2,"Yes",1991
example of style-2:
#num,str,num
1,Hello \
Word,1990
2,Yes,1991
example of style-3:
#num,str,num
1,Hello \nWorld,1990
2,Yes,1991
You need to use "\r". You can't use escaped characters (aside from \') in single quoted strings. '\n' and '\r' are a literal backslash followed by an n or r, while "\n" and "\r" are newlines and carriage returns respectively.
As for inserting new lines in your CSV file, it's up to your implementation. There is no standard for CSV, so you'll have to figure out what format to use based on the system you're supplying the CSV data to. Some might accept a '\n' sequence and interpret it as a new line, others might allow a literal newline provided the cell is enclosed in quotes, and still others will not accept new lines at all.
Created an Excel 2010 worksheet with 3 columns.
Added a heading row with literal values: one, two, three
Added 1 data row with literal values: abc, abc, abc except that within the 2nd column I pressed ALT+ENTER after each letter to create a carriage return and line feed.
Did SAVE AS > OTHER and choose CSV while ignoring the warnings.
Examined the CSV data using NOTEPAD++ and clicked the Show All Characters button in toolbar.
One can see the following:
one, two, three[CR][LF]
abc,"a[LF]
b[LF]
c",abc[CR][LF]
Hope this lends more clarify.

Categories