When you need to parse a single CSV record into fields, it helps to understand both the normal result and what changes with different inputs. This guide focuses on practical examples rather than repeating PHP background material.
str_getcsv provides the behavior described below. Quoted separators remain part of the field. CSV handling is not the same as splitting on commas.
What Is str_getcsv in PHP?
Parse a single csv record into fields. Quoted separators remain part of the field. CSV handling is not the same as splitting on commas.
Its syntax is:
str_getcsv(string $string, string $separator = ..., string $enclosure = ..., string $escape = ...): array
Parameters and return value: Read the argument names and optional values in the signature above. Methods use an existing object, while stand-alone functions take direct arguments. The examples demonstrate the relevant arguments and the value returned or the object modified.
Important: Supply the escape argument explicitly for interoperable CSV behavior. For files with multiple records use fgetcsv().
Example 1: Basic Usage
This first example demonstrates how to parse a single CSV record into fields with a small input.
<?php
print_r(str_getcsv('"Maya Chen","Paris, France",42', ",", "\"", ""));
Output:
Array
(
[0] => Maya Chen
[1] => Paris, France
[2] => 42
)
The example uses the simplest useful form. Quoted separators remain part of the field. CSV handling is not the same as splitting on commas. The output may depend on the active ICU locale, PHP configuration, or input supplied to the function.
Example 2: Another Case
Here the input or option changes, making it easier to see where the behavior differs. Supply the escape argument explicitly for interoperable CSV behavior. For files with multiple records use fgetcsv().
<?php
print_r(str_getcsv("red;green;blue", ";", "\"", ""));
Output:
Array
(
[0] => red
[1] => green
[2] => blue
)
Compare the result to the first example. explode() does not respect CSV quoting; fgetcsv() reads a record from a stream.
Practical Example: Applying the Function
This standalone example shows how the same operation fits into a small application task.
<?php
$csvRecord = "\"Starter, Plus\",19.95,active";
$fields = str_getcsv($csvRecord, ",", "\"", "");
foreach ($fields as $fieldIndex => $fieldValue) {
echo $fieldIndex . ": " . $fieldValue . PHP_EOL;
}
Output:
0: Starter, Plus
1: 19.95
2: active
The example keeps the operation visible and gives you a starting point for using it in an application. Supply the escape argument explicitly for interoperable CSV behavior. For files with multiple records use fgetcsv().
Important Behavior and Edge Cases
- Core behavior: Quoted separators remain part of the field. CSV handling is not the same as splitting on commas.
- Return value: Check the result separately from changes to passed-in arrays, iterators, or object state.
- Data constraints: Supply the escape argument explicitly for interoperable CSV behavior. For files with multiple records use fgetcsv().
- Environment: Run this example under a PHP version supporting the API; check results and exceptions before using them.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
str_getcsv | Parse a single csv record into fields. |
| Related PHP APIs | explode() does not respect CSV quoting; fgetcsv() reads a record from a stream. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Assuming the function works on every kind of input | Follow the declared argument types and check the input format before calling it. |
| Ignoring the behavior of the result | Supply the escape argument explicitly for interoperable CSV behavior. For files with multiple records use fgetcsv(). |
| Using the wrong related PHP API | explode() does not respect CSV quoting; fgetcsv() reads a record from a stream. |
Key Takeaways
str_getcsvhelps you parse a single CSV record into fields.- Quoted separators remain part of the field. CSV handling is not the same as splitting on commas.
- Supply the escape argument explicitly for interoperable CSV behavior. For files with multiple records use fgetcsv().
- explode() does not respect CSV quoting; fgetcsv() reads a record from a stream.
str_getcsv is useful when you need to parse a single CSV record into fields. Start with the small examples, then adapt the practical example while respecting its input and environment requirements.