3 changing delimiters in fputcsv PHP Code Examples in 2023

Last Updated on

CraftyTechie is reader-supported. When you buy through links on our site, we may earn an affiliate commission.

How to Change the Delimiters in fputcsv PHP Code Example

Delimiters in fputcsv can be modified using the separator argument. This article includes some examples related to changing delimiters in fputcsv.

<?php
$employees = [
    ['Id', 'Name', 'Age', 'Salary', 'Department'],
    ['1', 'Anna', '30', 20000, 'Finance'],
    ['2', 'Adam', '25', 15000, 'IT'],
    ['3', 'Bob', '32', 25000, 'Finance'],
    ['4', 'Cara', '20', 12000, 'Logistics'],
    ['5', 'Daniel', '28', 27000, 'Engineering'],
];
 
//Creates a new file employee_records.csv
$file = fopen('employee_records.csv', 'w'); //w is the flag for write mode.
 
if($file === false)
{
    die('Cannot open the file');
}
 
foreach($employees as $employee)
{
    //Formats the employee record as CSV and writes it out employee_records.csv
    fputcsv($file, $employee, '-');
}
 
//Closes the file.
fclose($file);
?>

CSV PHP Code Examples and Learning Path

Introduction 

PHP fputcsv is vital for handling reading and writing CSV in PHP. CSV or comma-separated files help persist tabular data. As the name suggests, a CSV file uses commas to separate data fields. So, by default, the fputcsv uses commas as a delimiter. Here’s a screenshot of a CSV file with multiple lines of data, also known as data records.

delimiters in fputcsv

However, the fputcsv in PHP provides an argument that can alter the defaults, including the default delimiter. This article includes examples of changing delimiters in fputcsv. But before that, it reviews the function and its arguments to reveal the right way of changing the default delimiter. So, let’s start with the function first.

PHP fputcsv: A Review

The fputcsv in PHP formats a line as CSV and writes it out to a file.

Function Signature

fputcsv( resource $stream, array $fields, string $separator = ",", string $enclosure = "\"", string $escape = "\\", string $eol = "\n"): int|false

So, the function has several arguments but the one that concerns the delimiter in fputcsv is the separator. The separator has the default value of a comma. So, it formats the field argument, which is fundamentally an array. The function writes the array elements to a file, separating them by the delimiter. Here’s an example of writing a CSV file in PHP.

Note: Only a single character can be specified as a delimiter. So, for instance, the ‘|’ is a valid delimiter in fputcsv but ‘||’ raises an error.

<?php
$employees = [
    ['Id', 'Name', 'Age', 'Salary', 'Department'],
    ['1', 'Anna', '30', 20000, 'Finance'],
    ['2', 'Adam', '25', 15000, 'IT'],
    ['3', 'Bob', '32', 25000, 'Finance'],
    ['4', 'Cara', '20', 12000, 'Logistics'],
    ['5', 'Daniel', '28', 27000, 'Engineering'],
];
 
//Creates a new file employee_records.csv
$file = fopen('employee_records.csv', 'w'); //w is the flag for write mode.
 
if($file === false)
{
    die('Cannot open the file');
}
 
foreach($employees as $employee)
{
    //Formats the employee record as CSV and writes it out employee_records.csv
    fputcsv($file, $employee);
}
 
//Closes the file.
fclose($file);
?>

Here’s the output. Observe that the delimiter is the comma.

Id,Name,Age,Salary,Department
1,Anna,30,20000,Finance
2,Adam,25,15000,IT
3,Bob,32,25000,Finance
4,Cara,20,12000,Logistics
5,Daniel,28,27000,Engineering

Using similar data, we will try to alter the delimiter in fputcsv.

#1- Dash delimiter in fputcsv

Here’s an example of using dash ‘-’ as the delimiter instead of a comma.

<?php
$employees = [
    ['Id', 'Name', 'Age', 'Salary', 'Department'],
    ['1', 'Anna', '30', 20000, 'Finance'],
    ['2', 'Adam', '25', 15000, 'IT'],
    ['3', 'Bob', '32', 25000, 'Finance'],
    ['4', 'Cara', '20', 12000, 'Logistics'],
    ['5', 'Daniel', '28', 27000, 'Engineering'],
];
 
//Creates a new file employee_records.csv
$file = fopen('employee_records.csv', 'w'); //w is the flag for write mode.
 
if($file === false)
{
    die('Cannot open the file');
}
 
foreach($employees as $employee)
{
    //Formats the employee record as CSV and writes it out employee_records.csv
    fputcsv($file, $employee, '-');
}
 
//Closes the file.
fclose($file);
?>

The third argument, the separator is the ‘-’ changing the delimiters in fputcsv. Here’s the output.

Id-Name-Age-Salary-Department
1-Anna-30-20000-Finance
2-Adam-25-15000-IT
3-Bob-32-25000-Finance
4-Cara-20-12000-Logistics
5-Daniel-28-27000-Engineering

#2 – Empty space delimiter in fputcsv

Here’s another example of using white space as the delimiter.

<?php
$employees = [
    ['Id', 'Name', 'Age', 'Salary', 'Department'],
    ['1', 'Anna', '30', 20000, 'Finance'],
    ['2', 'Adam', '25', 15000, 'IT'],
    ['3', 'Bob', '32', 25000, 'Finance'],
    ['4', 'Cara', '20', 12000, 'Logistics'],
    ['5', 'Daniel', '28', 27000, 'Engineering'],
];
 
//Creates a new file employee_records.csv
$file = fopen('employee_records.csv', 'w'); //w is the flag for write mode.
 
if($file === false)
{
    die('Cannot open the file');
}
 
foreach($employees as $employee)
{
    //Formats the employee record as CSV and writes it out employee_records.csv
    fputcsv($file, $employee, ' ');
}
 
//Closes the file.
fclose($file);
?>

Here’s the output.

Id Name Age Salary Department
1 Anna 30 20000 Finance
2 Adam 25 15000 IT
3 Bob 32 25000 Finance
4 Cara 20 12000 Logistics
5 Daniel 28 27000 Engineering

#3 – Slash delimiter in fputcsv

Another good option is using the pipe ‘/’ symbol as a delimiter.

<?php
$employees = [
    ['Id', 'Name', 'Age', 'Salary', 'Department'],
    ['1', 'Anna', '30', 20000, 'Finance'],
    ['2', 'Adam', '25', 15000, 'IT'],
    ['3', 'Bob', '32', 25000, 'Finance'],
    ['4', 'Cara', '20', 12000, 'Logistics'],
    ['5', 'Daniel', '28', 27000, 'Engineering'],
];
 
//Creates a new file employee_records.csv
$file = fopen('employee_records.csv', 'w'); //w is the flag for write mode.
 
if($file === false)
{
    die('Cannot open the file');
}
 
foreach($employees as $employee)
{
    //Formats the employee record as CSV and writes it out employee_records.csv
    fputcsv($file, $employee, '/');
}
 
//Closes the file.
fclose($file);
?>

Here’s the output.

Id/Name/Age/Salary/Department
1/Anna/30/20000/Finance
2/Adam/25/15000/IT
3/Bob/32/25000/Finance
4/Cara/20/12000/Logistics
5/Daniel/28/27000/Engineering

So, the fputcsv is a robust function and can use any delimiter just fine as long as it is a single character. 

Changing Delimeters in PHP with fputcsv

This article includes examples of changing delimiters in fputcsv function. The article also reviews the PHP fputcsv function for the clarity of readers. The fputcsv in PHP provides a separator argument for modifying the delimiter. We hope that you’ve learned something new today. Keep learning about PHP through intuitive and in-depth articles and tutorials at FuelingPHP.

Want to learn more about PHP?

We have many fun articles related to PHP. You can explore these to learn more about PHP.

Did you find this article helpful?

Join the best weekly newsletter where I deliver content on building better web applications. I curate the best tips, strategies, news & resources to help you develop highly-scalable and results-driven applications.

Build Better Web Apps

I hope you're enjoying this article.

Get the best content on building better web apps delivered to you.