Output A csv file in PHP {code ...} for example, the content of $ line is CA, 12, YMU, 13, YMU, 12, N. The data I want: & amp; quot; CA & amp; quot;, & amp; quot; 12 & amp; quot;, & amp; quot; Y & amp; quot; MU & amp; quot;, & amp; quot; 13 & amp; quot;, & amp; quot; Y & amp; quot; MU & amp; quot;, & amp; quot; 12 & amp; qu... output A csv file using PHP
foreach ($old['lines'] as $line) { fputcsv($new, $line);}
For example, the content of $ line is:
CA, 12, Y
MU, 13, Y
MU, 12, N
Expected data:
"CA", "12", "Y"
"MU", "13", "Y"
"MU", "12", "N"
I do this
foreach ($lines as &$line) { foreach ($line as &$column) { $column = '"' . $column . '"'; }}
As a result, two double quotation marks are added to each column of data:
"" CA "", "12" "," "Y ""
"" MU "", "13" "," "Y ""
"" MU "", "12" "," "N ""
This method is not correct. Is there another better method?
Reply content:
Output A csv file using PHP
foreach ($old['lines'] as $line) { fputcsv($new, $line);}
For example, the content of $ line is:
CA, 12, Y
MU, 13, Y
MU, 12, N
Expected data:
"CA", "12", "Y"
"MU", "13", "Y"
"MU", "12", "N"
I do this
foreach ($lines as &$line) { foreach ($line as &$column) { $column = '"' . $column . '"'; }}
As a result, two double quotation marks are added to each column of data:
"" CA "", "12" "," "Y ""
"" MU "", "13" "," "Y ""
"" MU "", "12" "," "N ""
This method is not correct. Is there another better method?
I always think that fputcsv () is quite earthy... It is not very troublesome to implement it myself...
Of course, if you need to use multiple double quotation marks, we should first study why this problem occurs...
/* enclose a field that contains a delimiter, an enclosure character, or a newline */if (FPUTCSV_FLD_CHK(delimiter) ||FPUTCSV_FLD_CHK(enclosure) ||FPUTCSV_FLD_CHK(escape_char) ||FPUTCSV_FLD_CHK('\n') ||FPUTCSV_FLD_CHK('\r') ||FPUTCSV_FLD_CHK('\t') ||FPUTCSV_FLD_CHK(' ')) {
/Ext/standard/file. c1867th-1875 rows...
It is described hereenclosure...DelimiterRules...
Double quotation marks are the default enclose character... triggers the rule... causes double quotation marks to be added outside the double quotation marks, resulting in the result of two double quotation marks...
Knowing the principles... Let's study the countermeasures...
The first method... use the default double quotation mark as the surround character... but force all elements to trigger...
foreach ($lines as &$line) { foreach ($line as &$column) { $column = $column . ' '; }}
The result is probably similar to the following...
"CA", "12", "Y"
"MU", "13", "Y"
"MU", "12", "N"
The second method... Let's study the manual... the instructions on this function are like this...
Int Fputcsv( Resource$ Handle, Array$ Fields [, String$ Delimiter = ','[, String$ Enclosure = '"'])
You can see that this function supports the custom surround character... that is, the fourth parameter $ enclosure...
So we can manually add double quotation marks on both sides of all elements... and modify the default surround character to be empty...
foreach ( $old['lines'] as $line ) { fputcsv( $new, $line, ',', ' ' );}
This method allows your example to return perfect results...
However, if the element triggers a rule for the occurrence of the surround character, a space will appear at both ends of the separator, that is, the comma...
You may wonder why it must be a space... because the delimiter must be a character... it cannot be empty or null...
If you are sure that your element does not contain all the characters that will trigger the occurrence of the surround character and another character...
You can also change it to another character... anyway, you can try to never trigger the rule for adding a bucket...
The two methods are not perfect... either of them depends on your needs...
Of course, in the end, I still want to say... this function is too earthy. Let's implement it by myself...
The method is not formal. If a field contains double quotation marks, an error occurs during parsing:
"1","2", "b c" ,"'ss""
The fputcsv method has four parameters:
fputcsv(file,fields,seperator,enclosure);
File Name
Fields data array
The separator of the seperator field. The default Delimiter is comma.
Enclosure field delimiter. The default value is double quotation marks.
The point of your problem is the 4th parameter enclosure. Because you have not passed the parameter (you do not actually need to pass it), the default value is double quotation marks. Therefore, you are added twice after additional processing.