How can I split a CSV file in PHP? - php

I have a big CSV file. I want to separate this file into separate files based on the value in one of the fields.
This is what I have done. Using fgetcsv I convert the CSV into an array, and using in_array, I check the content and display if it contains the string within the array.
I will be getting the comparison string from another text file iteratively to check whether it is contained in the csv. In this case I have specified it as "Testing".
Below is the code:
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if(in_array("Testing", $data))
{
var_dump($data);
}
}
fclose($handle);
}
This is working, but now I am stuck. How do I write $data into another CSV file? Or is there a better way to do this?

It's actually pretty simple and if the string just has to be on the line, you don't even need fgetcsv. Just
$srcFile = new SplFileObject('test.csv');
$destFile = new SplFileObject('new.csv', 'w+');
foreach ($srcFile as $line) {
if (strpos($line, 'testing') !== FALSE) {
$destFile->fwrite($line);
}
}
This will create two file objects. The first one holding the content of your source file. The second one creating an all new file for the lines containing your search string. We then just iterate over each line and check if the search string exists. If so, we write it to destination file.
The source file will not be touched this way. If you want to have one file with the search string and one file without, just create a third SplFileObject and add an else block to the if writing the line to that one then. In the end, delete the source csv file.

You have to do some tricky thing I am providing some basic idea for doing so, here is the code:
//opening file
if ($fp = fopen('log.csv', 'r')) {
$line_number = 0;
//loop for Reading file as line by line csv file
while ($line = fgetcsv($fp, 0, ';')) {
if ($line_number++ == 0) {
continue;
}
//array data string to make possible to provide file name
//according to column name required
$date = explode(' ', $line[0]);
//Change the column name according to your needs
$file = $date[0] .'.log';
file_put_contents(
//change the folder name according to your needs
'monthly/'. $file,
//printing data in appended file
implode(';', $line) ."\n",
FILE_APPEND
);
}
//closing file
fclose($fp);
}
It reads CSV file line by line, extracts date part from the first column and creates new file and appends data to it.
Note:
folder "monthly" must be writable

Related

Read a tabulator seperated csv with PHP

I'm not able to read a tabulator seperarated csv file (and yes, i know its csv and no tsv and the c is for tabulator...) with php and seperate it right. When i give out my imported Data with echo or readfile all the tabulators are replaced by a space and i can't use space as a sperator.
Actually i'm a bit confused that this problem is not very common when i use Google, so maybe i'm the problem...
The Problem exists with XAMPP v3.2.3 and PHP Version 7.3.5
$tempFile = fopen($tempFilePath, "r");
$uploadData = fread($tempFile, filesize($tempFilePath));
fclose($tempFile);
echo $uploadData;
$uploadData = str_replace('"','',$uploadData);
$uploadData = str_replace('\r\n','\n',$uploadData);
$uploadData = str_replace('\r','\n',$uploadData);
$uploadData = str_replace(';',',',$uploadData);
$uploadData = str_replace('\t',',',$uploadData); //Here i'm trying to replace the tabulator with a colon to work with it afterwards
When reading a CSV file in PHP, it is best to use the fgetcsv function. With it you can specify the deliminator of the file. The function will output a row of the file. Your deliminator should be "\t".
You should use fgetcsv function which already has functionality to read tsv.
Like this:
$uploadData = [];
if (($handle = fopen("test.csv", "r")) !== false) {
while (($data = fgetcsv($handle, 0, "\t")) !== false) {
$uploadData[] = $data;
}
fclose($handle);
}
If you want to convert tsv to csv you can use inverse function fputcsv:
$fh = fopen('file.csv');
foreach ($uploadData as $datum) {
fputcsv($fh, $datum);
}
fclose($fh);
If you want automatically detect csv delimiter maybe this article can help you.

similar to simplexml_load_file but for csv in php

When a client upload an xml file, the following piece of code catch it "on the fly", and perform several operations without actually saving the file in the server (just a temp file I guess):
if(isset($_POST['AddXmlElement'])) {
$xml = simplexml_load_file($_FILES['NewRecordXml']['tmp_name']);
foreach( $xml->records->record as $key1) {
//Do stuff;
}
}
I would like to do the same, but with a csv file. Is there an instruction like "simplecsv_load_file" that can be used?
Consider using fopen() to open a read connection on the csv upload:
$handle = fopen($_FILES["csvfile"]["tmp_name"], "r");
// LOOP ITERATES THROUGH ALL ROWS OF CSV FILE
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
echo $data[0]; // FIRST COMMA-SEPARATED CONTENT
echo $data[1]; // SECOND COMMA-SEPARATED CONTENT
...
}
fclose($handle);

Convert a CSV file to force encapsilation

My ideal fix would be a function that can take a CSV file that does not have forced encapsulation (no quotes around values if the value has no spaces or is just a number) and convert it into a CSV file that makes sure every field is encapsulated with double quotes.
<?php
$raw_file = BASE_DIR."pathto/csv.csv";
$fixed_file = BASE_DIR."pathto/fixed.csv";
convert_file($raw_file, $fixed_file);
//move on with life!!
?>
Thanks for you help!
Use fgetcsv to get the contents of your original csv file and fputcsv (using the fourth parameter) to build the encapsulated file.
For example, supposing your column separator is ; :
<?php
$raw_file = BASE_DIR."pathto/csv.csv";
$fixed_file = BASE_DIR."pathto/fixed.csv";
// Getting contents
$raw_handle = fopen($raw_file, 'r');
$contents = array();
while (($data = fgetcsv($raw_handle, 0, ';')) !== false) {
$contents[] = $data;
}
fclose($raw_handle);
// Putting contents
$fixed_handle = fopen($fixed_file, 'w');
foreach ($contents as $line) {
fputcsv($fixed_handle, $line, ';', '"');
}
fclose($fixed_handle);
//move on with life!!
?>

fgetcsv skip blank lines in file

I have this script that I did, it basically grabs all the files in my "logs" folder and merge them all in one array file, my only problem is that, sometimes the script breaks if there is blank line or empty line! how can I tell it to automatically skip blank empty lines and go to next? blank lines are not necessarily at the top or bottom! could be in the middle of the csv file
<?php
$csv = array();
$files = glob('../logs/*.*');
$out = fopen("newfile.txt", "w");
foreach($files as $file){
$in = fopen($file, "r");
while (($result = fgetcsv($in)) !== false)
{
$csv[] = $result;
}
fclose($in);
fclose($out);
}
print json_encode(array('aaData' => $csv ));
?>
As you can read in the documentation for fgetcsv():
A blank line in a CSV file will be returned as an array comprising a single null field, and will not be treated as an error.
Checking for that before adding it to your data array should be sufficient:
while (($result = fgetcsv($in)) !== false) {
if (array(null) !== $result) { // ignore blank lines
$csv[] = $result;
}
}
This works 100% tested, simplest way. The explanation is that blank lines make fgetcsv return a non-empty array with just a null element inside.
if ($result[0] == NULL)
continue;
In short
$csv = array_map('str_getcsv', file($file_path, FILE_SKIP_EMPTY_LINES|FILE_IGNORE_NEW_LINES));
Explanation
file reads the content of the file into an array. The FILE_SKIP_EMPTY_LINES will skip the empty lines in the file.
array_map will apply the function str_getcsv on each element of the array. str_getcsv will parse the string input for fields in
csv format and return an array containing the fields.
Read more about str_getcsv
Read more about file
Read more about array_map

Read .csv file and save its values in a list of arrays

I am new at php programming but I have been stuck with this code for some time.
I would like to read a .csv file line by line and then save its values in a list of arrays.
$file = fopen('Sub-Companies.csv', 'r');
while (($line =
fgetcsv($file)) !== FALSE) {
print_r($line);
list($customer_id[],$company_name[],$department[],$employee[],$country[],$zipcode[],$address[],$city[],
$smth1[], $smth2[], $phone_no1[],$phone_no2[],$email[],$website[],
$customer_no[],$problem1[],$problem2[]) = explode(";",$line); }
fclose($file); var_dump($customer_id);
The problem is that, although it is read correctly the file, then the explode is not working and the arrays appear to be null.
One thing that I am considering is that some arrays have more ";" than others, so that might be a problem, that is why I have the arrays $problem1 and $problem2, in order to store the values of this arrays.
Any help would be great!
You're using fgetcsv() in the wrong way.
We've come to this solution while chatting here on StackOverflow.
<?php
// Create file data.csv with your data
$handle = fopen('Sub-Companies.csv', 'r');
$customer_id = array();
$xyz_array = array();
// ...
// Better use a specified length (second parameter) instead of 0
// It slows down the whole process of reading the data!
while (($line = fgetcsv($handle, 0, ';')) !== FALSE) {
$customer_id[] = $line[0];
$xyz_array[] = $line[1];
}

Categories