UPLOAD CSV with special encoding (fgetcsv) - php

I have the problem with the php function fgetcsv() .
Lets start from the begin . I want to export csv files in ISO encoding and upload csv . With the export csv i dont have problem but with upload csv i have . This code runs perfect when i have not special characters . If i have greek characters he cannot read it and print NULL. If you can find me a solution !!!
Thanks for your time !!!
$csv = array(); $keys = array();
if (($handle = fopen($uploadedcsv, "r")) !== FALSE) {
while (($lines = fgetcsv($handle, 0, $delimiter)) !== FALSE) {
if ($row == 0) {
utf8_encode($keys);
utf8_encode($lines);
mb_convert_encoding($keys,'ISO-8859-15','utf-8');
mb_convert_encoding($lines,'ISO-8859-15','utf-8');
$keys = $lines;
} else {
utf8_encode($keys);
utf8_encode($lines);
mb_convert_encoding($keys,'ISO-8859-15','utf-8');
mb_convert_encoding($lines,'ISO-8859-15','utf-8');
$csv[] = array_combine($keys, $lines);
}
$row++;
}
fclose($handle);`

Related

PHP fgetcsv with not valid CSV file

I have a CSV file with content:
Центральный;Московская область;117036;PC;JEEP;GRAND CHEROKEE;ДЖИП ГРАНД ЧЕРОКИ;В;01.01.0001;08.05.2014;2004;1J8G2Е8N94У161064;-;1J8G2Е8N94У161064;-;94;2;227;4701;1;1;29;2495;2073;ФИЗ ЛИЦО;-;АКАДЕМИЧЕСКИЙ (ЮЗАО) Р-Н;-;-;-
Центральный;Московская область;117036;PC;VAZ;LARGUS;ЛАДА ЛАРГУС FS015L;В;01.01.0001;08.05.2014;2014;ХТАFS015LЕ0811430;UА46515;ХТАFS015LЕ0811430;-;11;1;84;1598;1;1;09;2010;1260;"АЗИЯ МАТЕРИАЛ ХЭНДЛИНГ ООО;7728814946;АКАДЕМИЧЕСКИЙ (ЮЗАО) Р-Н;-;КРЖИЖАНОВСКОГО;2|21
Центральный;Московская область;117208;PC;TOYOTA;LANDCRUISER;ТОЙОТА ЛЕНД КРУЗЕР ПРАДО;В;01.01.0001;08.05.2014;2001;JТЕВN99J100077420;1122027;JТЕВN99J100077420;JТЕВN99J100077420;94;2;178;3378;1;1;21;2680;1900;ФИЗ ЛИЦО;-;ЧЕРТАНОВО СЕВЕРНОЕ (ЮАО) Р-Н;-;-;-
I tried import this CSV to SQL DB with this while loop:
while (($csv_data = fgetcsv($csv_file, 10000, ';', '"')) !== false) {
//some stuff
}
In this CSV file I have 3 records, but in SQL I see only 2. Problem in this line:
;1260;"АЗИЯ МАТЕРИАЛ ХЭНДЛИНГ ООО;7728814946;
Problen in extra character " before АЗИЯ. How I can correctly parse this CSV file? File is more then 200 Mb.
===== EDIT =======
Okay, obviously your CSV gets not parsed right I used different data and thought CSV is CSV and that example worked just as fine idk. Now it works with your data. Only problem is your ", this is causing problems
(I replaced your first element because I don't speak Russian and I cannot see differences in words)
$file_data = array_filter(explode("\n", file_get_contents("data.csv")));
$data = array();
foreach ($file_data as $key => $row) {
$data[] = str_getcsv($row, ';', '', "\\");
}
print_r($data);
===== OLD MESSAGE =======
I tried this PHP example and it worked just as fine.
/*
$row = 1;
if (($handle = fopen("data.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in roe $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
*/

Gravity forms: exporting two forms of entries data to one CSV file

I'm wondering if I can export two deferent forms entries based on the submitter id but with a specific concept,
The concept is:
If the entry title from the first form == some entry field of the other form, let's show these two entries in the exported data in one row, and so on, something like array_map in php.
I hope that you got the idea, any help to do that?
Thanks a lot,
Magdi
Use These Codes:
(first code returns data from csv file into an array)
(second code gets data from an array and save into a csv file)
function csvExporter($path) {
$csv = array();
$row = 1;
if (($handle = fopen($path, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$csv[str_pad($row,4,0,STR_PAD_LEFT)] = array();
for ($c = 0; $c < $num; $c++) {
$csv[str_pad($row,4,0,STR_PAD_LEFT)][$c] = $data[$c];
}
$row++;
}
fclose($handle);
}
return $csv;
}
function csvImporter($path,$array) {
$csvFile = fopen($path,'w+');
foreach ($array as $rows) {
$eachRow = '[';
foreach ($rows as $columns) {
$eachRow .= '\'' . trim($columns,'\'') .'\',';
}
$eachRow .= ']' . PHP_EOL;
}
fclose($csvFile);
}

Saving specific columns of CSV to another CSV file in PHP

so trying to read a csv file and extracting details from the file and wish to store specific columns of the csv to another csv file.
I have managed to extract the specific columns of the input csv file but not really sure how I can write those extracted columns to another csv file.
Here is the part of my work:
$row = 1;
if (($handle = fopen("testtable.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$row++;
echo $data[0] . "\n" . $data[1] . "<br />\n";
}
$file = fopen("editedTable.csv","w");
foreach ( $data as $line) { //working on this part of the code
fputcsv($file, explode(',',$line));
}
fclose($handle);
}
Obviously there is an error in the foreach loop as I am trying to work out the logic.
The data that is extracted is basically first and the second column (data[0], data[1]). Just need this to write to another CSV file. The input csv is just a comma delimited file with 5 columns. Any idea how I can modify the foreach loop and the use the fputcsv method?
Thanks
fputcsv accepts an array on the second parameter.
I think this is what you looking for:
<?php
$file = fopen("editedTable.csv","w");
$row = 1;
if (($handle = fopen("testtable.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000)) !== FALSE) {
$num = count($data);
$row++;
echo $data[0] . "\n" . $data[1] . "<br />\n";
fputcsv($file, [$data[0], $data[1]]);
}
}
fclose($handle);
fclose($file);
Because you place the fputs outside of the $data initial loop, you catch only one row, here is the modified code.
$row = 1;
$file = fopen("editedTable.csv","w");
if (($handle = fopen("testtable.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$row++;
echo $data[0] . "\n" . $data[1] . "<br />\n";
$line = $data[0].", ".$data[1];
$line .= "\n";
fputs($file, $line);
}
fclose($handle);
}
fclose($file);

skip columns while converting tab delimited text file to csv php

I am trying to convert a tab delimited file to csv. The problem is its a huge file. 100000 plus records. And i want only specific columns from that file. The file is not generated by me but by amazon so i cant really control the format.
The code i made works fine. But i need to ignore/remove some columns or rather i want only few columns from that. How do i do that without effecting the performance of conversion from txt to csv.
$file = fopen($file_name.'.txt','w+');
fwrite($file,$report);
fclose($file);
$handle = fopen($file_name.".txt", "r");
$lines = [];
$row_count=0;
$array_count = 0;
$uid = array($user_id);
if (($handle = fopen($file_name.".txt", "r")) !== FALSE)
{
while (($data = fgetcsv($handle, 100000, "\t")) !== FALSE)
{
if($row_count>0)
{
$lines[] = str_replace(",","<c>",$data);
array_push($lines[$array_count],$user_id);
$array_count++;
}
$row_count++;
}
fclose($handle);
}
$fp = fopen($file_name.'.csv', 'w');
foreach ($lines as $line)
{
fputcsv($fp, $line);
}
fclose($fp);
I am using unset to remove any column. But is there a better way ? for multiple columns.
I would do that by checking keys. For example:
// columns keys you don't wanna skip
$keys = array(0, 1, 3, 4, 7, 9);
$lines = file($file_name);
$result_lines = array();
foreach ($lines as $line) {
$tmp = array();
$tabs = explode("\t", $line);
foreach($tabs as $key => $value){
if(in_array($key, $keys)){
$tmp[] = $value;
}
}
$result_lines[] = implode(",", $tmp);
}
$finalString = implode("\n", $result_lines);
// Then write string to file
Hope it helps.
Cheers,
Siniša
In its simplest form i.e. without worrying about removing columns from the output this will do a simple read line and write line, therefore no need to maintain any memory hungry arrays.
$file_name = 'tst';
if ( ($f_in = fopen($file_name.".txt", "r")) === FALSE) {
echo 'Cannot find inpout file';
exit;
}
if ( ($f_out = fopen($file_name.'.csv', 'w')) === FALSE ) {
echo 'Cannot open output file';
exit;
}
while ($data = fgetcsv($f_in, 8000, "\t")) {
fputcsv($f_out, $data, ',', '"');
}
fclose($f_in);
fclose($f_out);
This is one way of removing the unwanted columns
$file_name = 'tst';
if ( ($f_in = fopen("tst.txt", "r")) === FALSE) {
echo 'Cannot find inpout file';
exit;
}
if ( ($f_out = fopen($file_name.'.csv', 'w')) === FALSE ) {
echo 'Cannot open output file';
exit;
}
$unwanted = [26,27]; //index of unwanted columns
while ($data = fgetcsv($f_in, 8000, "\t")) {
// remove unwanted columns
foreach($unwanted as $i) {
unset($data[$i]);
}
fputcsv($f_out, $data, ',', '"');
}
fclose($f_in);
fclose($f_out);

Ignore CSV Column

Hi for some reason my following code isnt working:
if (($handle = fopen('https://www.national-lottery.co.uk/player/euromillions/results/downloadResultsCSV.ftl', 'r')) === false) {
die('Error opening file');
}
$headers = fgetcsv($handle, 1024, ',');
$complete = array();
while ($row = fgetcsv($handle, 1024, ',', "'")) {
$complete[] = array_combine($headers, $row);
++$row;
}
fclose($handle);
I think it's to do with the data in the CSV file (the column Raffle having quotation marks in?) - is there a way I can ignore this column Raffle?
$headers is not being properly populated due to there being an empty line at the beginning of the file. Try the following, which reads multiple lines until it reaches the header:
if (($handle = fopen('https://www.national-lottery.co.uk/player/euromillions/results/downloadResultsCSV.ftl', 'r')) === false) {
die('Error opening file');
}
do {
$headers = fgetcsv($handle, 1024);
} while (is_array($headers) && count($headers) != 9);
$complete = array();
while ($row = fgetcsv($handle, 1024)) {
$complete[] = array_combine($headers, $row);
++$row;
}
fclose($handle);
The enclosure of this CSV file is " not '
You define the single quotation mark as enclosure, but the source file has no enclosure:
Based on:
$row = fgetcsv($handle, 1024, ',', "'")
PHP expects your data to be in this format:
'07-Jun-2013','14','26','45','50','7','2','7','LSH166797'
but the file is in this format:
07-Jun-2013,14,26,45,50,7,2,7,LSH166797
There might be other problems in your code, I didn't dig too deep.
To avoid hassles, you can just use fgets and explode() each line.

Categories