str_getcsv
(PHP 5 >= 5.3.0, PHP 7, PHP 8)
Parse a CSV string into an array
Description
Parses a string input for fields in CSV format and returns an array containing the fields read.
Parameters
stringThe string to parse.
Escape-parameter
Return Values
Returns an indexed array containing the fields read.
Changelog
| Version | Description |
|---|---|
| 8.4.0 | Now throws a ValueError if separator, enclosure, or escape is invalid. This mimics the behavior of fgetcsv() and fputcsv(). |
| 7.4.0 | The escape parameter now interprets an empty string as signal to disable the proprietary escape mechanism. Formerly, an empty string was treated like the default parameter value. |
Examples
str_getcsv() example
<?php
$string = 'PHP,Java,Python,Kotlin,Swift';
$data = str_getcsv($string, escape: '\\');
var_dump($data);
?>The above example will output:
array(5) {
[0]=>
string(3) "PHP"
[1]=>
string(4) "Java"
[2]=>
string(6) "Python"
[3]=>
string(6) "Kotlin"
[4]=>
string(5) "Swift"
}str_getcsv() example with an empty string
On an empty string this function returns the value [null] instead of an empty array.
<?php
$string = '';
$data = str_getcsv($string, escape: '\\');
var_dump($data);
?>The above example will output:
array(1) {
[0]=>
NULL
}