Pergunta

I am trying to parse a csv file in PHP. My problem is the following: If there is a field stating with "é" or "í", the parser eats all those characters from the start of a field.

The problem is only present on my host, it's not present when using XAAMP locally (newer PHP version). The PHP version on my host with the bug is: 5.2.6-1+lenny9

The code is nothing but one line of fgetcsv.

while (($program = fgetcsv($handle, 0, ',', '"')) !== FALSE) {...}

This code already outputs the "eaten" version, for example when viewed by print_r.

Is there anything I can do? It must be a bug in PHP something, which has been fixed since then. One alternative option I found out was to just escape the sequence, by putting a comma at the end of a field (my csv source, Google Spreadsheets automatically wraps the field in " " if there is a , present inside). Then I can write a function that deletes the last character if it's a comma (any help on this?).

Is is (or was it) a known bug in PHP, and were there any solutions for this? If not, can you help me with the delete-last-character-if-its-a-comma function?

Foi útil?

Solução

Your actual problem is that the webserver runs under a locale which forbids multibyte charsets. If set to C I get the same result:

<?php print_r(str_getcsv("ée, íi, zz, bb, "));

$   LC_ALL=C   php test_getcsv.php

Cuts of the é and í in fields. [0] => e [1] => i [2] => zz

But when I run it like this:

$   LC_ALL=de_DE.UTF-8  php test_getcsv.php

I get the correct results. [0] => ée [1] => íi [2] => zz

You will need to investigate which locales are available on your server, then use setlocale(LC_ALL, "xy_zz.UTF-8") at the start of your script.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top