Previously summary:
The company needs to make a new product. Export includes details of products but the product details contain a lot of CSV separators;
It was only necessary to wrap the content in double quotes "" In normal steps, but in subsequent tests
When you open a file by using Microsoft Excel after exporting, the product details still appear as a result of the dislocation caused by the delimiter
The random branches that we don't want to see.
Here make a little note to prevent forgetting later
PHP generates CSV
1 Setting Header
Header("CONTENT-TYPE:TEXT/CSV; Charset=utf-8 ");//defines the file type of the output CSV, encoded as Utf-8 Header("content-disposition:attachment;filename={$file _name}");//set up attachment download, "Generate file name" = Custom Header("Content-description:file Transfer");//just file transfer no specific meaning Header(' Content-type:text/comma-separated-values;charset=utf-8 ');//Make sure the CSV file text is delimited by a comma delimiter Header(' cache-control:must-revalidate,post-check=0,pre-check=0 ');//Update cache in a timely manner Header(' expires:0 ');//set cache has expired Header(' Pragma:public ');//Cache All Information Header(' content-transfer-encoding:binary ');//do not differentiate between encoding and receiving any data that is transmitted
2 garbled problem with UTF8 open for Chinese and Mac OS
Add BOM Header
Print (chr(0xEF). CHR (0xBB). CHR (0xBF))
Tips: source de ask Community Dong Chen's answer
To identify Unicode files, Microsoft recommends that all Unicode files begin with the ZERO WIDTH nobreak space character.
This acts as a "signature" or "byte order mark (Byte-order Mark,bom)" To identify the encoding and byte order (Big-endian or Little-endian) used in the file.
The specific correspondence is shown in the table below.
Bytes Encoding Form
FE FF UTF-32, Big-endian
FF FE xx UTF-32, Little-endian
FE FF UTF-16, Big-endian
FF FE UTF-16, Little-endian
EF BB BF UTF-8
Encoded in UTF-8 no BOM format, so you want to export the UTF-8 CSV file that Microsoft Excel can display correctly, you need an explicit output BOM
3 misalignment caused by large text separators
A CSV is a text that is separated by a delimiter, and is not a capacity-based argument for a particular lattice.
But when we use Excel to open CSV, one of the Excel cells has a capacity limit.
As far as I can get, the result is about 32,000 characters, so I need to cut large text when I'm outputting large text.
Each box has a capacity of 32000 to effectively avoid the occurrence of each of the lattice has been processed but there is still text in the case of a newline and a column
Tips: PHP handling is posted here
Use Mb_strlen to get the correct number of characters
$len = Mb_strlen ($description);
Use the MB function here, if it contains Chinese, use the MB_SUBSTR function, otherwise the interception will be incorrect $description _p1 = mb_strcut ($description, 0, 32000 ); $description _p2 = mb_strcut ($description
PHP generated CSV encountered delimiter problem