0

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
)
16
  • Are you basically asking how to deal with commas nested inside of quotes? Commented Nov 21, 2020 at 3:14
  • yes i need to parse that data as single value inside nested quotes Commented Nov 21, 2020 at 3:17
  • Ok. Well i'd suggest renaming the title of your question to "regex replace commas inside of quotes" or something similar. You will need to replace the commas inside of quotes before you do the csv parser command. Commented Nov 21, 2020 at 3:19
  • 1
    @GetSet If the CSV parsing is set up right, it should properly handle quotes. Commented Nov 21, 2020 at 3:29
  • 1
    Per @tadman, Code Mania you can specify an "enclosure". I haven't tested whether this option will ignore on those fields that dont have the "enclosure" but you sure can. Heres the doc ref php.net/manual/en/function.fgetcsv.php Commented Nov 21, 2020 at 3:35

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

12 Comments

Thanks , it working fine from reading contents from txt file . I have csv file csv file has still issue. Is there any way to read csv and write that content as text file
can you please try same content with csv comma delimited . It is not getting . I have data's in csv format
Interesting. But how would fgetscsv() know the file extension when a handle is being passed? Possibly a difference in PHP versions to do the expected deed.
@CodeMania, a .csv file is a plain text file. The extension shouldn't matter but apparently it does. Which PHP ver are you using?
@cully output while ready from csv file ``` 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=�M�=�M� [12] => 2020-11-01T06:34:23Z [13] => 1604212463397 [14] => ""Hi Raju [15] => Thank you so much"" [16] => 497230" ) ```
|
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.