diff options
author | Urban Müller | 2017-05-22 11:17:40 +0200 |
---|---|---|
committer | Urban Müller | 2017-05-22 11:18:39 +0200 |
commit | ca8d809d46670a124fed16a14b06505f1ec7a60c (patch) | |
tree | 2d0ee455cf2274c03372f738efcd89ce4a062067 | |
parent | fd44cd53734a7b1a18343e11682a548f63ebbc50 (diff) | |
download | itools-ca8d809d46670a124fed16a14b06505f1ec7a60c.tar.gz itools-ca8d809d46670a124fed16a14b06505f1ec7a60c.tar.bz2 itools-ca8d809d46670a124fed16a14b06505f1ec7a60c.zip |
introduce fixcolnames in ::csv
-rw-r--r-- | it_pipe.class | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/it_pipe.class b/it_pipe.class index 2ab931b..281ee5f 100644 --- a/it_pipe.class +++ b/it_pipe.class @@ -160,16 +160,18 @@ function cols($collist, $separator = "\t") /** * Return contents of pipe as associative records. Column titles read from first line, separators can be \t or ; or , - * @param $forceschema ignore schema in file, replace it with this comma separated column list + * @param $p['forceschema'] ignore schema in file, replace it with this comma separated column list + * @param $p['fixcolnames'] replace non-identifier chars in colum names */ -function csv($forceschema = null) +function csv($p = []) { + $p = is_string($p) ? ['forceschema' => $p] : $p; $counts = count_chars($this->lines[0]); $splitchar = $counts[ord("\t")] ? "\t" : ($counts[ord(";")] > $counts[ord(",")] ? ";" : ","); $csvhead = array_shift($this->lines); - $cols = $forceschema ? explode(",", $forceschema) : str_getcsv(trim($csvhead, "#\n "), $splitchar, '"'); # should function_exists('str_getcsv') - foreach (preg_grep('/^$/', $cols) as $idx => $dummy) - $cols[$idx] = "field$idx"; # replace empty column names + $cols = $p['forceschema'] ? explode(",", $p['forceschema']) : str_getcsv(trim($csvhead, "#\n "), $splitchar, '"'); # should function_exists('str_getcsv') + foreach ($cols as $idx => $col) + $cols[$idx] = it::replace(['^$' => "field$idx", '\W' => $p['fixcolnames'] ? "_" : '$0'], $col); $oldlocale = setlocale(LC_CTYPE, "0"); setlocale(LC_CTYPE, 'de_CH.iso-8859-1'); # this works for utf-8 as well |