Improved CSV import delimiter scan

Technical discussion for developers and service providers

Improved CSV import delimiter scan

Postby infinity » 11:39pm, Sat 11 Jun, 2011

Hi, everyone,

using PHPlist with external sources was a little disapointing for me because I have a large table of users in google docs which I want to import in PHPList. The problem is that google uses "," as delimiter - and some fields also have a comma inside. Fields with commas are in "field", an improved script could decide weather it is a seperation delimiter or a in-field delimiter-char. So I wrote a small patch for PHPList 2.10.14 (current stable). Only affected file is commonlib/pages/importcsv.php - heres the diff:

Code: Select all
324,327c324,347
<     # get rid of text delimiters generally added by spreadsheet apps
<     $line = str_replace('"','',$line);
<
<     $values = explode($_SESSION["import_field_delimiter"],$line);
---
>
>     // detects weather field delimiter is in or out of a field which is limited by " ". prevents script to use delimiter-chars which are part of the field-strings as delimiters to seperate fields
>     $values = Array();
>     $infield = false;
>     $field = "";
>     $j = mb_strlen($line);
>     for ($k = 0; $k < $j; $k++) {
>       $char = mb_substr($line, $k, 1);
>       if ($char == "\"") {
>         if ($infield)
>           $infield = false;
>         else
>           $infield = true;
>       }
>       else if ($char == $_SESSION["import_field_delimiter"] && !$infield) {
>         $values[] = $field;
>         $field = "";
>       }
>       else {
>         $field .= $char;
>       }
>     }
>     if ($field)
>       $values[] = $field;


I'm sure you can make this faster somehow, but it works great (for me ;) ). If you want to integrate the code into phplist feel free to do this - I don't think I'm the only one with such problems. :)

Thanks for your great project!
infinity
phpList newbie
 
Posts: 1
Joined: 11:20pm, Sat 11 Jun, 2011

Re: Improved CSV import delimiter scan

Postby CS2 » 1:25pm, Fri 30 Sep, 2011

A substantial improvement over the current code, though I'd prefer fgetcsv().
CS2
PL Master
 
Posts: 216
Joined: 2:20am, Wed 04 Feb, 2009


Return to Developers Space

Who is online

Users browsing this forum: No registered users and 1 guest