What is PHP str_getcsv() Function?
If you want to parse a comma separated values (CSV) of a string into an array, use PHP str_getcsv() function.
Syntax:
str_getcsv(string, separator, enclosure, escape)
Parameters:
The Function has 1 required parameter and 3 optional parameters-
string (Required): It specifies a CSV string.
separator (Optional): It specifies a single-byte character which is used as value separator (or delimiter). Its default value is comma (,).
enclosure (Optional): It specifies a single-byte character which is used to enclose values. Its default value is double quote (“).
escape (Optional): It specifies a single-byte character which is used to escape special characters. Its default value is backslash (\).
Return Values:
The function returns an indexed array that contains the values parsed from the CSV string.
Practical Usages of str_getcsv() Function:
The function has many practical usages in many areas. Some includes-
- Parsing from database fields.
- Parsing CSV input from forms.
- Configuration files
- Handling CSV formatted API data.
- Parsing few CSV lines from a file.
Notes on str_getcsv() Function:
- The str_getcsv() function is the string version of the fgetcsv() function. The fgetcsv() function reads from a file that contains numerous CSV strings.
- If your string has multiple lines, you can separate the lines by explode(“\n”) function and then read each line by through a loop. See example below-
<pre> <?php $string = '"HTML","CSS","JavaScript" "UI","UX","Figma"'; $lines = explode("\n", $string); foreach ($lines as $line) { $array = str_getcsv($line); print_r($array); } ?> </pre>Output:
Array
(
[0] => HTML
[1] => CSS
[2] => JavaScript
)
Array
(
[0] => UI
[1] => UX
[2] => Figma
) - The function handles separators inside an enclosure correctly. In the example below, the separator (,) is used in the 3rd value. And, still, the function doesn’t treat a separator inside an enclosure as a separator.
<pre> <?php $csv = '"Number","Type","Array, Nested Array"'; $result = str_getcsv($csv); print_r($result); ?> </pre>
Output:
Array
(
[0] => Number
[1] => Type
[2] => Array, Nested Array
) - You can use custom separator. In the example below, “|” is used as a custom separator.
<pre> <?php $string = "PHP|MySQL|JavaScript|REACT"; $result = str_getcsv($string, "|"); print_r($result); ?> </pre>
Output:
Array
(
[0] => PHP
[1] => MySQL
[2] => JavaScript
[3] => REACT
) - Empty CSV fields become empty strings, not null.
- Make sure that separator, enclosure, and escape characters are single-byte characters.
Caution:
For parsing large CSV file, don’t use str_getcsv() function, instead use fgetcsv() function as it is this function is memory efficient.
Why you choose str_getcsv() over explode() Function:
Though both function divide a string based on a separator, the biggest reason to select str_getcsv() is that inside an enclosure, it doesn’t consider a separator as a separator. So, a value can contain a separator. Check example below-
<pre>
<?php
$string = '"Number","Type","Array, Nested Array"';
$result = str_getcsv($string);
echo "The CSV string is: " . $string . "<br />";
echo "Result in str_getcsv() function-" . "<br />";
print_r($result);
echo "<br />";
echo "Result in explode() function-" . "<br />";
$result = explode(",", $string);
print_r($result);
?>
</pre>
Output:
The CSV string is: "Number","Type","Array, Nested Array"
Result in str_getcsv() function-
Array
(
[0] => Number
[1] => Type
[2] => Array, Nested Array
)
Result in explode() function-
Array
(
[0] => "Number"
[1] => "Type"
[2] => "Array
[3] => Nested Array"
)
Moreover, you can use enclosure, escape characters in str_getcsv() function, but, you can’t getthese benefit in explode function.
PHP Version Support:
PHP 5 >= 5.3.0, PHP 7, PHP 8
Summary: PHP str_getcsv() Function
The str_getcsv() function is one of the built-in string function in PHP. Use this powerful tool to parse a CSV string into an array.