Ignore CSV Column - php

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.

Related

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);

UPLOAD CSV with special encoding (fgetcsv)

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);`

CSV to Json with header row as key

I would like to convert a CSV to Json, use the header row as a key, and each line as object. How do I go about doing this?
----------------------------------CSV---------------------------------
InvKey,DocNum,CardCode
11704,1611704,BENV1072
11703,1611703,BENV1073
---------------------------------PHP-----------------------------------
if (($handle = fopen('upload/BEN-new.csv'. '', "r")) !== FALSE) {
while (($row_array = fgetcsv($handle, 1024, ","))) {
while ($val != '') {
foreach ($row_array as $key => $val) {
$row_array[] = $val;
}
}
$complete[] = $row_array;
}
fclose($handle);
}
echo json_encode($complete);
Just read the first line separately and merge it into every row:
if (($handle = fopen('upload/BEN-new.csv', 'r')) === false) {
die('Error opening file');
}
$headers = fgetcsv($handle, 1024, ',');
$complete = array();
while ($row = fgetcsv($handle, 1024, ',')) {
$complete[] = array_combine($headers, $row);
}
fclose($handle);
echo json_encode($complete);
I find myself converting csv strings to arrays or objects every few months.
I created a class because I'm lazy and dont like copy/pasting code.
This class will convert a csv string to custom class objects:
Convert csv string to arrays or objects in PHP
$feed="https://gist.githubusercontent.com/devfaysal/9143ca22afcbf252d521f5bf2bdc6194/raw/ec46f6c2017325345e7df2483d8829231049bce8/data.csv";
//Read the csv and return as array
$data = array_map('str_getcsv', file($feed));
//Get the first raw as the key
$keys = array_shift($data);
//Add label to each value
$newArray = array_map(function($values) use ($keys){
return array_combine($keys, $values);
}, $data);
// Print it out as JSON
header('Content-Type: application/json');
echo json_encode($newArray);
Main gist:
https://gist.github.com/devfaysal/9143ca22afcbf252d521f5bf2bdc6194
For those who'd like things spelled out a little more + some room to further parse any row / column without additional loops:
function csv_to_json_byheader($filename){
$json = array();
if (($handle = fopen($filename, "r")) !== FALSE) {
$rownum = 0;
$header = array();
while (($row = fgetcsv($handle, 1024, ",")) !== FALSE) {
if ($rownum === 0) {
for($i=0; $i < count($row); $i++){
// maybe you want to strip special characters or merge duplicate columns here?
$header[$i] = trim($row[$i]);
}
} else {
if (count($row) === count($header)) {
$rowJson = array();
foreach($header as $i=>$head) {
// maybe handle special row/cell parsing here, per column header
$rowJson[$head] = $row[$i];
}
array_push($json, $rowJson);
}
}
$rownum++;
}
fclose($handle);
}
return $json;
}

How to add columns to CSV using PHP

I need to add columns to an existing csv file ,but i can't find any solution to the problem.I have used "\t" and chr(9) to create columns but no success so please help me by providing me the right solution if any one can
Try this, and have a look at fgetcsv() and fputcsv() in the manual
<?php
$newCsvData = array();
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$data[] = 'New Column';
$newCsvData[] = $data;
}
fclose($handle);
}
$handle = fopen('test.csv', 'w');
foreach ($newCsvData as $line) {
fputcsv($handle, $line);
}
fclose($handle);
?>
Could you try using \r\n instead of \n ?

Categories