T
T
TopClans2017-12-27 16:24:20
PHP
TopClans, 2017-12-27 16:24:20

Why does fputcsv wrap strings in quotes?

The script collects data from different .xls files into an array, which is then immediately uploaded to CSV.
There are 2 types of files, both .XLS, but the structure is slightly different: in one, the lines are combined, so you have to mess around with them; besides, all the same, one of these lines stores several necessary data at once, well at least separated by the "|" symbol.
One way or another, the same array is obtained from both types of files: Article, manufacturer, price, quantity.
Examples of arrays, from two different files:

Array
(
    [1] => Array
        (
            [sku] => KR0867M
            [maker] => KRAUF
            [name] => Модуль в сборе с бензонасосом
            [quantity] => 1
            [price] => 2508
        )

    [2] => Array
        (
            [sku] => SMD030764
            [maker] => CHERY
            [name] => Прокладка помпы
            [quantity] => 1
            [price] => 75
        )
)

Array
(
    [1] => Array
        (
            [sku] =>  2300235K01J 
            [maker] => GREAT WALL 
            [name] =>  ПОДШИПНИК РОЛИКОВЫЙ ШЕСТЕРНИ БЛОКИРОВКИ ПЕРЕДНЕГО МОСТА HOVER  - 2300235-K01
            [quantity] => 1
            [price] => 117.08
        )

    [2] => Array
        (
            [sku] =>  2310012K01 
            [maker] => GREAT WALL 
            [name] =>  ШЕСТЕРНЯ БЛОКИРОВКИ ПЕРЕДНЕГО МОСТА HOVER
            [quantity] => 1
            [price] => 1266.19
        )
)

But for some reason, when exporting, some array elements are framed with quotation marks and a space, and some are not.
Here is the code for writing to .CSV:
$filename = explode(".", $file);
$output = fopen('csv/'.$filename[0].'.csv', 'w');
fputs($output, chr(0xEF) . chr(0xBB) . chr(0xBF)); // BOM
foreach ($row as $key) 
{
        fputcsv($output, $key, ';');
}

Here is what is written to .CSV files, respectively:
KR0867M;KRAUF;"Модуль в сборе с бензонасосом";1;2508
SMD030764;CHERY;"Прокладка помпы";1;75

" 2300235K01J ";"GREAT WALL ";" ПОДШИПНИК РОЛИКОВЫЙ ШЕСТЕРНИ БЛОКИРОВКИ ПЕРЕДНЕГО МОСТА HOVER  - 2300235-K01";1;117.08
" 2310012K01 ";"GREAT WALL ";" ШЕСТЕРНЯ БЛОКИРОВКИ ПЕРЕДНЕГО МОСТА HOVER";1;1266.19

What annoys me is that in one case only the name of the nomenclature is wrapped in quotation marks, and in the other - also the article and the manufacturer.
I tried to look at the type of the variable with the manufacturer: gettype($key['maker']) - gives out that in all cases string, but why the hell is string wrapped in quotes in one case, and not in the other?
I don't understand what's the problem?
UPD: figured it out! I noticed that in one case there are quotes and a space in the file, in the other - just quotes. I saw that when dividing the string, I did not get rid of the space. the trim function removed the spaces and solved the problem

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question