0

I have a csv, generated with Open Office Spreadsheet, separator is comma, text wrapper is ".

I want just to columns in my spreadsheet, as it is some key-value list, like:

a -> b
aa -> bb
aaa -> bbb

so my spreadsheet has just two columns, each row containing a key-value pair, each key in a cell, each value in a cell. But when I use

if ($_FILES ['userfile'] ['error'] == UPLOAD_ERR_OK &&
    is_uploaded_file ( $_FILES ['userfile'] ['tmp_name'] )) {

        $csvfile = file_get_contents ( $_FILES ['userfile'] ['tmp_name'] );
    }

    $dict = str_getcsv($csvfile);

And var_dump $dict afterwards, i get

array(4) { [0]=> string(1) "a" [1]=> string(6) "b "aa"" [2]=> 
            string(8) "bb "aaa"" [3]=> string(3) "bbb" }

See index 1, where the first value "b" and the seconde key "aa" are stored in one index.

OpenOffice places no comma behind the values, aka before a new row. How do I avoid that two strings end up in one index? Can I somehow tell str_getcsv that when two " follow each other, it's also a separator? Or can I add a third column always containing # and say "if it contains #, its a line break? (I tried, and couldn't manage..) Or is there even a function, that expects key, value, linebreak in csv?

Whats the correct way to do this? I'm sure people done this so often, but I don't know how to google it.

Thanks!

1 Answer 1

1

How about using fgetcsv() ? http://www.php.net/manual/en/function.fgetcsv.php

It reads line by line, there's an example in the link.

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

2 Comments

Um.. Well.. this.. does it.. :) Thanks ^^ ( I missed the forest for the trees.. and never done that task before..)
You're welcome, I had my fair share of headaches with CSV and PHP :D

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.