PHP fgetcsv with not valid CSV file - php

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

Related

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.

CSV not properly being parsed on new lines

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!

how to extend the length of time when fgetcsv function of php is reading csv file?

am having a problem here, i read a csv file using fgetcsv function then loop it, and write it into an xml file...when am using the script in a not so big data, everything works perfectly and writes the xml file....but when i use it in a huge csv, the xml can't finished being written and end up in an unclosed token thing
if (($handle = fopen("C:\\xampp\htdocs\yii\branch\seo_tagsdev.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 10000000, ",")) !== FALSE) {
$num = count($data);
// echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++)
{
$url = $doc->createElement('url');
$root->appendChild($url);
$loc = $doc->createElement('loc',htmlentities($data[$c]));
$url->appendChild($loc);
$lastmod = $doc->createElement('lastmod',date('Y-m-d'));
$url->appendChild($lastmod);
$freq = $doc->createElement('changefreq','daily');
$url->appendChild($freq);
$priority = $doc->createElement('priority',0.9);
$url->appendChild($priority);
// echo $data[$c] . "<br />\n";
}
$doc->save("C:\\xampp\htdocs\yii\branch\seo_tagsdev.xml");
}
i even adjusted the length parameter of fgetcsv and yet it doesn't work at all

Tools / Scripts for importing csv to db table

In tried importing a csv into a table using phpMyAdmin and I'm getting permission errors so I'm looking for any php scripts available around that I can use.
What scripts are available? Can ane point to any free one's?
fgetcsv is the php function you should use.
For details see here.
Sample code here..
$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++) {
// write insert query here
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
Thanks.

Categories