Anyone share code in php to parse csv data . While am parsing data i got values inside double quotes as separate array so my logic will change . I need to store this data to mysql DB. My sample csv file is below
com,24,2.1.0.5,en,mido,2020-11-01T03:29:32Z,1604201372915,2020-11-01T03:29:32Z,1604201372915,5,,Nice๐๐,2020-11-01T06:34:23Z,1604212463397,"Hi Raju, Thank you so much",497230
com,24,2.1.0.5,en,athene_f,2020-11-01T04:19:52Z,1604204392095,2020-11-01T04:19:52Z,1604204392095,5,,So so,2020-11-01T06:33:58Z,1604212438170,"Hi dev, Thanks",497230
code
$csv_file = 'csv/test.csv';
$file = fopen($csv_file, "r");
fgetcsv($file);
$count=0;
while (($getData = fgetcsv($file, 10000, ",")) !== FALSE)
{
$getData = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $getData);
$getData = implode(",", $getData);
$getData = '\''.substr($getData , 1, -1).'\'';
$getData =explode(",", $getData);
$exp_array = explode(",", $getData);
$package_name = trim($getData[0]);
$app_version_code = trim($getData[1]);
$app_version_name = trim($getData[2]);
$reviewer_language = trim($getData[3]);
ob_get_clean();
}
expected output
Array
(
[0] => com
[1] => 24
[2] => 2.1.0.5
[3] => en
[4] => mido
[5] => 2020-11-01T03:29:32Z
[6] => 1604201372915
[7] => 2020-11-01T03:29:32Z
[8] => 1604201372915
[9] => 5
[10] =>
[11] => Nice๐๐
[12] => 2020-11-01T06:34:23Z
[13] => 1604212463397
[14] => Hi Raju, Thank you so much
[15] => 497230
)
I just ran this and got the output you indicated you were expecting:
<?php
$file = fopen('input.txt', "r");
while (($data = fgetcsv($file, 10000, ",")) !== FALSE) {
print_r($data);
}
Here's input.txt:
com,24,2.1.0.5,en,mido,2020-11-01T03:29:32Z,1604201372915,2020-11-01T03:29:32Z,1604201372915,5,,Nice๐๐,2020-11-01T06:34:23Z,1604212463397,"Hi Raju, Thank you so much",497230
com,24,2.1.0.5,en,athene_f,2020-11-01T04:19:52Z,1604204392095,2020-11-01T04:19:52Z,1604204392095,5,,So so,2020-11-01T06:33:58Z,1604212438170,"Hi dev, Thanks",497230
And here's the output:
Array
(
[0] => com
[1] => 24
[2] => 2.1.0.5
[3] => en
[4] => mido
[5] => 2020-11-01T03:29:32Z
[6] => 1604201372915
[7] => 2020-11-01T03:29:32Z
[8] => 1604201372915
[9] => 5
[10] =>
[11] => Nice๐๐
[12] => 2020-11-01T06:34:23Z
[13] => 1604212463397
[14] => Hi Raju, Thank you so much
[15] => 497230
)
Array
(
[0] => com
[1] => 24
[2] => 2.1.0.5
[3] => en
[4] => athene_f
[5] => 2020-11-01T04:19:52Z
[6] => 1604204392095
[7] => 2020-11-01T04:19:52Z
[8] => 1604204392095
[9] => 5
[10] =>
[11] => So so
[12] => 2020-11-01T06:33:58Z
[13] => 1604212438170
[14] => Hi dev, Thanks
[15] => 497230
)
If you are not getting the expected output, one other thing to check is the file encoding on your CSV file. It could be that your PHP script is expecting your file to be utf-8 encoded, but your file is using another encoding. There are lots of resources online for detecting file encoding and converting using PHP.
These 2 lines are 100% incorrect...
$getData =explode(",", $getData);
$exp_array = explode(",", $getData);
givn, that $getData contains a string, after explode() it will be an array (reassigned to $getData).
On the next line, this array is tried to be exploded again, but this will fail and give you warnings or errors.
Turn up error reporting level and display all errors on screen during development.
when i am trying to upload csv file in php and exporting fields in php using fgetcsv function, it is returned all result in one array instead of comma separated.
I have 2 csv file but in one its working and in another it is showing all values in one array
like this:
Array
(
[0] => Name
[1] => Number
[2] => Tax Center
John12
[3] => 2122561681
[4] => 71 West 19
John13
[5] => 6462102791
[6] => 72 West 19
John14
[7] => 9144190749
[8] => 73 West 19
John15
[9] => 8602867600
[10] => 74 West 19
John16
[11] => 8602435024
[12] => 75 West 19
John17
[13] => 8602429299
[14] => 76 West 19
John18
[15] => 2034838300
[16] => 77 West 19
here is my code to get only first row of csv
if(($handle = fopen($file , "r")) !== FALSE)
{
$fileAdded = true;
$adcon = 0;
while (($data = fgetcsv($handle, 0, ",")) !== FALSE)
{
/*echo '<pre>';
print_r($data);
echo '</pre>';*/
$first = $data;
break;
}
fclose($handle);
}
I'm trying to upload csv file, it uploads the csv, but the data are not correct.
How can I convert this into just string and remove the & lt;p> ; or the <p>?
Example of my csv file data:
**<p>\DECO APPLE GRN IN WOODEN CRATE SIZE 34X25X15.5CM PLASTIC</p>\**
You can do something like this.
$sample_string = '**<p>\DECO APPLE GRN IN WOODEN CRATE SIZE 34X25X15.5CM PLASTIC</p>\**';
echo strip_tags(html_entity_decode($sample_string));
// **\DECO APPLE GRN IN WOODEN CRATE SIZE 34X25X15.5CM PLASTIC\**
Application (if it's not malformed):
$data = array();
if (($handle = fopen('sample.csv', 'r')) !== false) {
while (($row = fgetcsv($handle, 4096, ',', "\n")) !== false) {
$data[] = array_map(function($var){
return strip_tags(html_entity_decode($var));
}, $row);
}
fclose($handle);
}
echo '<pre>';
print_r($data);
Output:
Array
(
[0] => Array
(
[0] => PROD.product_id
[1] => PROD.sync_2
[2] => PROD.quantity
[3] => PROD_DESC.name
[4] => PROD_DESC.description
[5] => PROD_DISC.price
)
[1] => Array
(
[0] => 1
[1] => ABCD
[2] => 2570
[3] => VIP
[4] => "\ DECO APPLE GRN IN WOODEN CRATE SIZE 34X25X15.5CM PLASTIC\"
[5] => 0.848
)
[2] => Array
(
[0] => 1
[1] => ABCDEFG
[2] => 2570
[3] => VIP 2
[4] => "\ DECO APPLE GRN IN WOODEN CRATE SIZE 34X25X15.5CM PLASTIC\"
[5] => 10
)
)
i create a method that will read a file with the application/octet type and here are some of the code.
Raw data :
GTHHS;MEKID Interface;5496;2012-07-20;
NM1;081;IN1;980898989;2001-01-15;Mr;Gaf;Uhkil;Uhkil,Gaf;PRI;Gaf
$contents = file_get_contents($tmp_filename);
$stringContents = explode(";", $contents);
Now it gives me this output :
Array
(
[0] => GTHHS
[1] => MEKID Interface
[2] => 5496
[3] => 2012-07-20
NM1
[4] => 081
[5] => IN1
[6] => 980898989
[7] => 2001-01-15
[8] => Mr
[9] => Gaf
[10] => Uhkil
[11] => Uhkil,Gaf
[12] => PRI
[13] => Gaf
PR1
[14] => 081
[15] => IN1
[16] => 20730089
[17] => 7 The Schooner
[18] => Auhaas
[19] => Huuula Ave
[20] =>
[21] => Kishma
PR2
[22] => 081
[23] => IN1
[24] => 232323233
[25] => 400006
[26] => HGD
[27] => M
[28] => M
[29] => 2007-10-16
[30] => 1976-03-31
);
How can i make the NM1, PR1 as the head of array like this :
Array (
[NM1] = array(
[0] => GTHHS
[1] => MEKID Interface
[2] => 5496
[3] => 2012-07-20
)
);
I am planning also to make the inner array [0]-[3] as json.
If you explode the contents by \n you have each line starting with that identifier. If you then just explode by ; in that line and add it as a sub array, you got it like you want.
This actually looks like a plain old CSV file with your ifentifier in line one. If so, try something like this:
$data = array();
if (($handle = fopen($filename, 'r')) !== FALSE)
{
while (($row = fgetcsv($handle, 1000, ";", "\"", "\n")) !== FALSE)
{
$key = array_shift($row);
$data[$key] = $row;
}
fclose($handle);
}
echo json_encode($data);
http://php.net/manual/en/function.str-getcsv.php
I have an interesting piece of work to complete
I have a lot of spreadsheets which I have converted to CSV's - these spreadsheets started out as templates with a set number of columns.
Unfortunately over time people have added columns and removed them.
Is there a way I can adapt the following code to select certain column names from each CSV
foreach($fileinfos as $pathname => $fileinfo) {
if (!$fileinfo->isFile()) continue;
if(substr($pathname,-3) == 'csv'){
$file = fopen($pathname, "r");
while ($line = fgetcsv($file,0,'^','ยฌ')){
echo substr($pathname,56) .' '.$numcols.'<br>';
}
fclose($file);
}
}
UPDATE:
Here is my array of accepted columns
Array
(
[0] => Date Raised
[1] => QA phase
[2] => Item ID
[3] => Screen Number
[4] => Reason
[5] => Matches originals?
[6] => Issue / Comments
[7] => Raise with Client?
[8] => Raised By
[9] => Status
[10] => TP/AS Status Initials
[11] => TP/AS Status date
[12] => TP/AS Status Comment
[13] => Retested by
[14] => Retested date
[15] => Retested Comment
)
here is my array of avalible
Array
(
[0] => Date Raised
[1] => QA phase
[2] => Item ID
[3] => Screen Number
[4] => exam
[5] => Reason
[6] => Matches originals?
[7] => Issue / Comments
[8] => Raise with Client?
[9] => Raised By
[10] => Status
[11] => TP/AS Status Initials
[12] => TP/AS Status date
[13] => TP/AS Status Comment
[14] => dd Comments
[15] => PM Comments
[16] => Retested by
[17] => Retested date
[18] => Retested Comment
)
Array combine dosnt work.
$file = fopen($pathname, 'r');
$headers = fgetcsv($file, 0, '^', 'ยฌ');
while ($line = fgetcsv($file, 0, '^', 'ยฌ')) {
$line = array_combine($headers, $line);
var_dump($line);
// access specific fields using $line['columnName']
}
I would try something like this:
$csv = array();
$columnnames = fgetcsv($fp, 1024);
while (true == ($columns = fgetcsv($fp, 1024))) {
$row = array_combine($columnnames, $columns);
$csv[] = $row;
}
On a CSV file like this
id,name,email
1,benjamin,benjamin#example.com
2,rob,rob#example.com
the $csv variable should contain something like this:
1 => array(
'id' => 1,
'name' => 'benjamin',
'email' => 'benjamin#example.com'
)
...