Skip to content
PHP Logo

What alternative PHP function could be used to handle CSV data with delimiter characters within text fields?

When handling CSV data with delimiter characters within text fields, the fgetcsv() function in PHP may not work as expected. To handle this issue, the str_getcsv() function can be used instead. This function allows for more flexibility in parsing CSV data by handling delimiter characters within text fields properly.

php
$csvData = '"John,Doe",25,"New York, NY"
"Jane,Smith",30,"Los Angeles, CA"';

$lines = explode(PHP_EOL, $csvData);

foreach($lines as $line){
    $data = str_getcsv($line);
    print_r($data);
}