Parse csv file that came from base64 on PHP

I am uploading a base64 encoded CSV file through an Ajax request to my server (an ajax request is a requirement). And I am trying to parse it so I have a multidimensional array.

The CSV file looks like this:

1, 1, 1, 1, 'Yuliana Arredondo', 'Mi razón social', 'snt@snt.snt', 'aoeaoeaoe1', 'avatar_56627eb7ea174.png', 'CUIH900419RC4', '2015-12-09', '2015-12-23', '2015-12-01', 'SYSTEM-CSV'
1, 1, 1, 1, 'Eduardo de la Cruz', 'Mi razón social', 'snt@snt.snt', 'aoeaoeaoe1', 'avatar_56627eb7ea174.png', 'CUIH900419RC6', '2015-12-09', '2015-12-23', '2015-12-01', 'SYSTEM-CSV'

(Each record is separated by an enter as far as I can tell)

And, on PHP I am trying:

    $b64 = $request->input('file');
    $pos  = strpos($b64, ';');
    if($pos !== false){
        $b64 = base64_decode(preg_replace('#^data:text/\w+;base64,#i', '', $b64));
$array = str_getcsv($b64);

But on that $array, instead of getting an array with 2 records (each one with 14 fields), I am getting a plain array with 28 records.

Am I doing something wrong? Or is it supposed to work like this? If so, is there any workaround?

On a side note, 14 fields is consistent, so it is valid to "hardcode" that as a constant.

Thanks in advance.


What about convertig your CSV string to an line array and then map str_getcsv to each line?

Something like this would be possible:

$lines = explode("\n", $b64);
$array = array_map('str_getcsv', $lines);

Instead of:

$array = str_getcsv($b64);

Hope it helps ;)

Need Your Help

Android Retrofit ParseSdk Geopoint

parse-platform retrofit

I have some place data in parse database.

Deploying Django to Heroku using a Windows machine (Production server NOT development server)

windows django deployment heroku gunicorn

I use a Windows machine and have a Django project that I have successfully deployed to Heroku, albeit using the development server. To use a production server Heroku seems to require 'Gunicorn' which