PHP str_getcsv() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, str_getcsv() parses one line of CSV text into an array.

It understands quoted fields, so commas inside quotes do not split the value.

Syntax

Call str_getcsv() with the values you need. Start with this line.

PHP
$row = str_getcsv($line);
CallWhat it does
str_getcsv("a,b,c")Returns ["a", "b", "c"].
str_getcsv("a;b;c", ";")Uses a semicolon as the separator.

It parses one record at a time. For a whole CSV file, use fgetcsv() or split the text into lines first.

Library declarations
PHP
str_getcsv(string $string, string $separator = ",", string $enclosure = "\"", string $escape = "\\"): array

Parameters

ParameterDescription
stringRequired. The CSV line to parse.
separatorOptional. The field separator. Default is a comma.
enclosureOptional. The quote character around fields. Default is a double quote.
escapeOptional. The escape character. Default is a backslash.

Return Value

Return ValueDescription
arrayAn array of field values from the line.

PHP Examples

Example 1 — Parse a Simple Line

Turn a comma-separated line into an array.

PHP
<?php
$row = str_getcsv("apple,banana,cherry");

print_r($row);

How It Works

  1. Pass a CSV line. Values are separated by commas.
  2. Default separator. A comma is used when you pass nothing else.
  3. Get an array. Each field is one element.
  4. Print it. print_r() shows the structure.

Example 2 — Commas Inside Quotes

Quoted fields can hold commas safely.

PHP
<?php
$row = str_getcsv('John,"New York, NY",30');

print_r($row);

How It Works

  1. Wrap the field in quotes. "New York, NY" stays together.
  2. The inner comma is kept. It is not treated as a separator.
  3. Quotes are removed. The value has no extra quote characters.
  4. explode() would fail here. It would split in the wrong place.

Example 3 — Parse Several Lines

Split the text into lines, then parse each one.

PHP
<?php
$csv = "name,age\nTom,30\nAnna,25";
$lines = explode("\n", $csv);

foreach ($lines as $i => $line) {
    if ($i === 0) {
        continue;
    }
    $cols = str_getcsv($line);
    echo $cols[0], " is ", $cols[1], PHP_EOL;
}

How It Works

  1. Split by newline. explode() gives one line per item.
  2. Skip the header. Index 0 holds the column names.
  3. Parse each row. str_getcsv() returns the fields.
  4. Use the columns. Print the name and age.

Frequently Asked Questions

str_getcsv() parses a line of CSV text and returns its fields as an array. It handles quoted values for you.
str_getcsv(string $string, string $separator = ",", string $enclosure = "\"", string $escape = "\\"): array. Only the first argument is required.
explode() splits blindly on a delimiter. str_getcsv() understands quotes, so a comma inside "New York, NY" stays in the same field.
Pass it as the second argument. For example, str_getcsv($line, ";") splits on semicolons.
It reads one record at a time. For files, use fgetcsv() in a loop, or split the text into lines before calling str_getcsv().

Next: str_increment()

str_increment() moves an alphanumeric string one step forward.

str_increment() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful