I have CSV file with special characters ØÅÆ
When I use fgetcsv, it just ignores the rest of the string. I tried var_dump for the row, but I got it like this: ���
This is the code that I have:
if (($handle = fopen($path, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
var_dump($data); die;
}
fclose($handle);
}
I tried adding this before my code setlocale(LC_ALL, 'en_US.UTF-8');
but without any luck. Do you have any advice?
Some sample data - first row from CSV:
VAR,E,5704617332886,500,11,"TURKISøåæØÅÆ","110"
The proper encoding should be ISO-8859-1. That was the problem
Related
I'm trying to upload a CSV file, read its contents and import that data into a database, but apparently I'm getting some kind of bug where an unknown character shows between each character.
This is my code:
$file = $request->file('file');
$fileName = $file->getRealPath();
$file = fopen($fileName, "r");
$i = 0;
while (($column = fgetcsv($file, 10000, ";")) !== FALSE) {
if ($i == 0){
$i++;
continue;
}
echo print_r($column);$i++;
}
fclose($file);
This is what I get from print_r:
Any ideas on what could it be? I've tried opening the CSV with encoding UTF8 and UTF16 but I still have this issue.
Thanks in advance.
Most likely it's \0 char that some system appends. Check with ord($string[0])
You can simply do str_replace("\0", '', $string)
Or try some regex replace: preg_replace('/[^\w\d-.,\s]/', '', $string)
There are many CSV file like the following:a.csv, b.csv, aab.csv etc.
They hold the same column and header. Now I want to put all the csv data into whole.csv. With only one header. How can I do it?
a.csv data:
header1 title post.....
test who posand
b.csv data:
header1 title post.....
head she pnow
etc .....
The whole.csv will contain all the csv data.
eg:
header1 title post.....
head she pnow
test who posand
I tried the following code.but not get I want to:
$csvs = glob("*.csv");
foreach($csvs as $csv) {
$row = 1;
if (($handle = fopen($csv, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$fp = fopen("whole.csv", 'w');
fputcsv($fp, $data);
$row++;
}
fclose($handle);
}
}
I have put all CSV files in the same directory.
For every input csv file you are opening the resultant csv file in write mode:
$fp = fopen("whole.csv", 'w');
which wipes the content of the whole.csv!!
You need to open the whole.csv file just once outside the loop and keep writing into it.
$csvs = glob("*.csv");
$fp = fopen("whole.csv", 'w');
foreach($csvs as $csv) {
$row = 1;
if (($handle = fopen($csv, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
fputcsv($fp, $data);
$row++;
}
fclose($handle);
}
}
Have a look at file_put_contents.
You would open each CSV file, then use file_put_contents passing whole.csv as the $filename parameter, the file handle as the $data parameter and use the FILE_APPEND flag to tell it to append the contents instead of overwriting.
Hi I have a fgetcsv function that reads a CSV file and exports the data into a list. This works, and I have used this function numerous times elsewhere with no problems.
The relevant bits of the code are:
ini_set("auto_detect_line_endings", true);
$file = fopen($_FILES['filename']['tmp_name'],"r");
while (($data = fgetcsv($file, 1000, ",")) !== FALSE)
{
$body_data['user_list'][] = $data;
}
fclose($file);
The problem is that the read CSV stops at any spaces (white space) between words in columned data.
I thought that auto_detect_line_endings would solve this, could it be that this is causing the problem?
Change your while loop with this
while (!feof($file) ) {
$body_data['user_list'][] = fgetcsv($file, 1024);
}
I am trying to search for a string match then split the file contents. After some trial and error, I am hoping someone can provide a helping hand. After fgetcsv, searching a string becomes almost pointless. Ideally, I would like to find each occurrence of "appointment", and split the contents with a comma.
if (($handle = fopen('file.csv', "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if(preg_match('/appointments/', $data, $matches, 0))
{
$split = preg_split("/,/", $data);
print_r($split);
}
}
fclose($handle);
}
The print statements gives back nothing.
What do you meant by splitting the content with a comma? Or do you want to add a comma after each "appointments"?
Instead of fgetcsv, use fread and replace "appointments" with "appointments, " using str_replace
$filename = 'file.csv';
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
$contents = str_replace('appointments', 'appointments, ', $contents);
echo $contents;
As noted, the file has ASCII characters which need cleaning before processing.
After getting the file and looping, strip the contents of ASCII character sets.
$stripAscii = preg_replace('/[^(\x20-\x7F)]*/','',$data);
$datePos = strpos($stripAscii,'Appointment:');
$data = substr($data, $datePos);
$data = str_replace('Appointment:', ' ', $row2);
Now, all fields after "Appointment:" is returned.
I'm importing a .CSV into an application with the following:
if (($handle = fopen($file, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
It sort of works but it's far from perfect. I wanted to first get out the row heads and put them into an array and then loop round each row to get the data sets in.
It seems to be having delimitting problems as the first row (heads) are also including a few parts of the second row.
I exported the .csv file straight from Excel. Wonder if there are encoding tricks etc I might be missing.
It sounds like your 1000 limit is not long enough; you should set it to a high enough value for that file ... or set to 0 for unlimited (not recommended, because it tends to be slower).
Set it to 4096 or 8192 first and see how it goes.
// use 8kB buffer for reading comma delimited line
while (($data = fgetcsv($handle, 8192, ",")) !== FALSE) {
Update
Okay, on second thought, perhaps you should inspect the file and confirm a few things:
Are the delimiters really a comma? (I guess you've already established this)
Are the string enclosures always double quotes?
Are strings escaped with a backslash (default) or another double quote?
fgetcsv doesnt always properly detect line endings.
Try using before the fgetcsv call:
ini_set('auto_detect_line_endings', true);
I changed to 8192 and got same results
while (($data = fgetcsv($handle, 8192, ",")) !== FALSE) {
...
}
and then did this one and got same results
$handle = fopen($_FILES['filename']['tmp_name'], "r");
ini_set('auto_detect_line_endings', true);
while (($data = fgetcsv($handle, 8192, ",")) !== FALSE) {
$data = array_map('mysql_real_escape_string', $data);
Why are you not using file()?
$trimmed = file('somefile.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);