CSV not properly being parsed on new lines - php

I have a CSV file that looks like:
Header1,Header2,Header3
value1,value2,value3
value11,value12,value13
value21,value22,value23
etc...
When I use this code to parse through it:
$csvData = file_get_contents($file_url);
$lines = explode(" ", $csvData);
$array = array();
foreach ($lines as $line) {
$array[] = str_getcsv($line);
}
dpm($array);
the first value of a line always ends up being part of the end of the last header:
EDIT: There must have been an issue with cache as this is working fine.

For reading CSV file explode() and file_get_contents() are not the correct options. Go for fgetcsv
You can do like below for reading CSV file:
<?php
$row = 1;
if (($handle = fopen("test.csv", "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);
}
?>

Use file instead of file_get_contents. That will return an array with one line of the input file on each index. This will work with all possible new line characters.
$lines = file($file_url, FILE_IGNORE_NEW_LINES)

You might want to break lines into newlines ("\n") instead of just spaces (" "). Even better, you might want to use file() to load file lines into an array. Better yet, you might want to use SplFileObject class to do that:
<?php
$file = new SplFileObject($file_url);
$file->setFlags(SplFileObject::READ_CSV | SplFileObject::SKIP_EMPTY
| SplFileObject::DROP_NEW_LINE | SplFileObject::READ_AHEAD);
$data = array();
foreach ($file as $row) {
$data[] = $row;
}
Good luck!

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);
}
*/

Reading CSV file data in PHP

I'm trying to create a page which pulls infoprmation from a CSV file, using PHP.
It should then populate a product list with the information.
This is the CSV file:
The code I have is as follows:
$file = file('data-example.csv');
$line = array("name", "id","price", "description");
foreach($file as $line) {
$csv = explode('//',$line);
$item = $csv;
print_r($item);
This is currently printing out the whole of the array. I'd like to know how I can access the individual nodes on the array, so that I could include them as variables within an HTML table.
Thanks
Ali
To amplify, and respond to the replies (thanks!):
This is the amended code, using fgetcsv:
if (($handle = fopen("data.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
echo($data[1][1]);
}
fclose($handle);
}
This seems to work, but returns the column contents. This is the data (data.csv):
"Dandelion","30","13.99","Dandelion 30 "
"Daisy","40","20.99","Daisy 40 "
"Cress","27","10.99","Cress 27 "
"Coral","17","6.99","Coral 17 ",
"Honeysuckle","40","17.99","Honeysuckle 40 "
"Rose","27","9.99","Rose 27"
"
(there's a new line at dandelion, Daisy,Cress,Coral,Honeysuckle and Rose)
When I try echo($data[1]); it gets the whole column as described, i.e 30 40 27 17 40 27 but echo($data[1][1]); returns "342142", and echo($data[1][1][1]); creates an error.
I don't understand it as this is an array?
Thanks
Ali
You should use native php functions to read csv files, such as str_getcsv
Something like:
$Data = str_getcsv($CsvString, "\n"); //parse the rows
foreach($Data as &$Row){
$Row = str_getcsv($Row, ";"); //parse the items in rows
}
Alternatively you can use fgetcsv that parses the line it reads for fields in CSV format and returns an array containing the fields read, i.e.:
<?php
$row = 1;
if (($handle = fopen("test.csv", "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);
}
?>

PHP Get values from CSV into an array

I have the following csv file:
9 carat gold,11.87
18 carat gold,23.73
Silver,0.49
Platinum,27.52
I would like to put this in a Multidimensional Array but i have no idea how to.
Thanks for any help.
Try this code
$csv = file_get_contents($file);
$data = explode(PHP_EOL, $csv);
$arr = array();
foreach ($data as $entry) {
$arr[] = str_getcsv($entry);
}
print_r($arr);
You can go through this link for more details about str_getcsv function
You can use fgetcsv to parse a CSV file without having to worry about parsing it yourself.
Example from PHP Manual:
$row = 1;
if (($handle = fopen("test.csv", "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);
}
You can use fgetcsv to read a csv file open with fopen.

Encapsulating and reading into an array from text file

I'm trying to make 2 classes, to encapsulate these text files, Customer.txt and Orders.txt. The customer one I need to read each line into a separate instance of an array for each line of text, how can I do this?
Also, do I have to do something about the comma's in the text files?
I know I need to use fopen, just not sure how to go about doing it.
This is what I've tried so far.
<?php
class readOrders{
$array_of_lines = fopen("orders.txt", 'r') or die("Failed to create file");
foreach($array_of_lines as $line) {
$pieces_of_line = explode(',', $line);
}
class readCustomers{
$array_of_more_lines = fopen("customers.txt", 'r') or die("Failed to create file");
foreach($array_of_more_lines as $line) {
$pieces_of_line = explode(',', $line);
}
?>
Here are my text files
Orders.txt
1,4,0133360903,Building Java Programs,Computer Science
2,6,0321836995,Mathematics All Around,Mathematics
3,24,0321825721,Mathematics for Elementary Teachers with Activity Manual,Mathematics
4,10,0133011208,Business Math,Business
customers.txt
2,Leonie,Köhler,leonekohler#surfeu.de,University of Stuttgart,Theodor-Heuss-Straße 34,Stuttgart,,Germany,70174,+49 0711 2842222
3,Bjørn,Hansen,bjorn.hansen#yahoo.no,University of Oslo,Ullevålsveien 14,Oslo,,Norway,0171,+47 22 44 22 22
4,François,Tremblay,ftremblay#gmail.com,McGill University,1498 rue Bélanger,Montréal,QC,Canada,H2G 1A7,+1 (514) 721-4711
I think fgetcsv will be useful here, example from php-doc:
<?php
$row = 1;
if (($handle = fopen("test.csv", "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);
}
?>

Reading CSV in PHP and out put its contents by array value

I want to display the contents of a CSV file by assigning it to a variable and then display it using echo statement.
This code is not working could someone point the error in it
$fh = fopen('db.csv', 'r');
$now = time();
$data=fgetcsv($fh);
$data[0]=$name;
echo $name;
Am a newbie to coding and scripting.
Thanks
This is the CSV line that I want to be printed
katz,26-11-2011,http//www.google.com
Why you first valorize the $data with an array of your CSV and then you overwrite the first position with the $name variable (which apparently is null)?
$data[0] = $name;
should be
$name = $data[0];
You could use the handy fgetcvs function for this.
Might as well post an example for want of a complete answer, shamelessly ripped from that same
manpage:
<?php
$row = 1;
if (($handle = fopen("test.csv", "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);
}
?>
Hope that fits your needs. Happy coding.

Categories