T2theC
T2theC

Reputation: 540

PHP remove all newlines and spaces from a csv

I am trying to convert a CSV to JSON using this awesome Jist: https://gist.github.com/robflaherty/1185299

I have been given a very CSV from a very antiquated procedural programming language. The CSV can ONLY be produced as follows.

I need to strip out the second line which is a blank newline and ideally, I'd like to strip out all the extra spaces for each line:

Series,           SKU,                           Stock

 01000      ,     01000-1116             ,          98
 01000      ,     01000-1132             ,           0
 01000      ,     01000-116              ,        1000
 01000      ,     01000-1164             ,        3880
 01000      ,     01000-12               ,        2040
 01000      ,     01000-132              ,        2240
 01000      ,     01000-1332             ,         545
 01000      ,     01000-1364             ,          50

I've tried every combination in my small arsenal to get this to work. preg_replace, trim etc.

Has anyone got any advice? Here is the section of the Jist that is handling the rows:

function csvToArray($file, $delimiter) {
    if (($handle = fopen($file, 'r')) !== FALSE) {

        $i = 0;
        while (($lineArray = fgetcsv($handle, 4000, $delimiter, '"')) !== FALSE) {
            for ($j = 0; $j < count($lineArray); $j++) {
                $arr[$i][$j] = $lineArray[$j];
            }
            $i++;
        }
        fclose($handle);
    }
    return $arr;
}

I hope someone can help! Thanks in advance for taking a look.

Upvotes: 1

Views: 2672

Answers (3)

Indrajeet Singh
Indrajeet Singh

Reputation: 2989

Try this code:
function csvToArray($file, $delimiter)
{
    if (($handle = fopen($file, 'r')) !== FALSE) {

        $i = 0;
        while (($lineArray = fgetcsv($handle, 4000, $delimiter, '"')) !== FALSE) {
            for ($j = 0; $j < count($lineArray); $j++) {
                if ($lineArray[$j] !== '') {
                    $arr[$i][$j] = $lineArray[$j];
                }
            }
            $i++;
        }
        fclose($handle);
    }
    return $arr;
}

Upvotes: 1

Skriptotajs
Skriptotajs

Reputation: 874

Try this. It skips empty lines and trims values.

// Function to convert CSV into associative array
function csvToArray($file, $delimiter) { 
  if (($handle = fopen($file, 'r')) !== FALSE) { 
    $i = 0; 
    while (($lineArray = fgetcsv($handle, 4000, $delimiter, '"')) !== FALSE) { 
      if(count($lineArray)==1) continue; //skip empty line (will fail if file will have only one column)
      for ($j = 0; $j < count($lineArray); $j++) { 
        $arr[$i][$j] = trim($lineArray[$j]); //trim value
      } 
      $i++; 
    } 
    fclose($handle); 
  } 
  return $arr; 
} 

Upvotes: 1

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111899

You need to check $lineArray and use trim() function as in the following code:

function csvToArray($file, $delimiter) {
    if (($handle = fopen($file, 'r')) !== FALSE) {

        $i = 0;
        while (($lineArray = fgetcsv($handle, 4000, $delimiter, '"')) !== FALSE) {
            if (count($lineArray) == 1 && is_null($lineArray[0])) {
                continue;
            }
            for ($j = 0; $j < count($lineArray); $j++) {
                $arr[$i][$j] = trim($lineArray[$j]);
            }
            $i++;
        }
        fclose($handle);
    }
    return $arr;
}

Upvotes: 2

Related Questions