1

File csv

Page with results

Code:

<?php
$file_name = 'test.csv';

if (($handle_f = fopen($file_name, "r")) !== FALSE)
{

while ( ($data_f = fgetcsv($handle_f, 20000, ";"))!== FALSE) {
echo Name - '.$data_f[0].', City - '.$data_f[1].', Adress - '.$data_f[2].', Site - '.$data_f[3].'<br>';
}

fclose($handle_f);
} else {$err = 1; echo "File not open";}
?>

For example in result i would be get next(for example first 2 rows):

Name - Сибвез, City - Абакан, Adress - ул. Советская, 44, Site - www.sibvez.ru
Name - Быттехника, City - Абакан, Adress - ул. Дружбы народов, д. 52, Site - www.sibvez.ru

But now in results first 2 rows next:

Name - , City - , Adress - . Советская, 44, Site - www.sibvez.ru
Name - , City - , Adress - . Дружбы народов, д. 52, Site - http://www.bytech.ru

Tell me please why i cannt get correctly results?

13
  • Probably because you used the wrong charset. Commented Jan 15, 2014 at 14:12
  • @YUNOWORK charset file .csv an file .php is UTF-8 without bom. tell me please where error? Commented Jan 15, 2014 at 14:14
  • It would help if you post an example of the CSV file. Commented Jan 15, 2014 at 14:15
  • y, but as far as i know, .csv usually uses some windows-3410 (or whatever) charset, not UTF-8, thats why you get these encoding errors. Commented Jan 15, 2014 at 14:16
  • @Tuga see start question please. i give all links(link on csv invait.ru/ExPRoG/TestCsvDump/test.csv) Commented Jan 15, 2014 at 14:16

2 Answers 2

1

you are need set correct locale - use setlocale(LC_ALL, 'ru_RU.CP1251');

Full code:

setlocale(LC_ALL, 'ru_RU.CP1251');
echo strtoupper('SERVER USE LOCALE ru_RU.CP1251');

$file_name = 'test.csv';

if (($handle_f = fopen($file_name, "r")) !== FALSE)
{

while ( ($data_f = fgetcsv($handle_f, 20000, ";"))!== FALSE) {
echo 'Name - '.$data_f[0].', City - '.$data_f[1].', Adress - '.$data_f[2].', Site - '.$data_f[3].'<br>';
}

fclose($handle_f);
} else {$err = 1; echo "File not open";}

Reference for setlocale: https://www.php.net/manual/en/function.setlocale.php

Sign up to request clarification or add additional context in comments.

4 Comments

In practice, would you reset the locale back to the original value after parsing the file?
Would it be safer to use LC_CTYPE instead of LC_ALL?
@MarcAudet my practice show that if not use setlocale(LC_ALL, 'ru_RU.CP1251'); in start file, data will be got not correct. But many hostings in USA not has it locale(ru_RU.CP1251).
@MarcAudet need read information about LC_CTYPE becouse i all use LC_ALL. It single answer which i can get 2 years ago.
0

There seems to be a syntax error here:

while ( ($data_f = fgetcsv($handle_f, 20000, ";"))!== FALSE) {
echo Name - '.$data_f[0].', City - '.$data_f[1].', Adress - '.$data_f[2].', Site -        '.$data_f[3].'<br>';
}

Should be:

while ( ($data_f = fgetcsv($handle_f, 20000, ";"))!== FALSE) {
echo 'Name - '.$data_f[0].', City - '.$data_f[1].', Adress - '.$data_f[2].', Site -        '.$data_f[3].'<br>';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.