What am I doing wrong?
You called the function with the wrong parameters ...
http://php.net/manual/en/function.str-getcsv.php:
delimiter
Set the field delimiter (one character only).
The second parameter is the field delimiter, and that is ;
, not the newline.
$rows = '7;"Hi
";3';
$array = str_getcsv($rows,';','"');
var_dump($array);
// output:
array(3) {
[0]=>
string(1) "7"
[1]=>
string(4) "Hi
"
[2]=>
string(1) "3"
}
EDIT:
The result should be array with just one value
Why one value? What is your actual field delimiter character supposed to be here? I assumed it was the ;
... but if you expect to get only one value from the shown example input, then what is the delimiter? Or how is it even CSV …?
2nd Edit:
first I need to get lines - field delimiter is a newline
That is not really how str_getcsv
works - that assumes that you have just one single line of your CSV data as your input string already. And you can’t really use something like a simple explode at newline to “get” single lines - because the newline character can also be inside the fields.
fgetcsv
would be the proper function to achieve both ... but that operates on files, not string data. Perhaps PHP’s various streams could help with that somehow, if you “write” your string data into a temp file or memory, so that you could actually use fopen
to get a readable stream that fgetcsv
can then work with ... this question can give you an idea of how that could work: how to use fgetcsv with strings