Open In App

PHP SplFileObject getCsvControl() Function

Last Updated : 28 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The SplFileObject::getCsvControl() is an inbuilt function in PHP that retrieves the current CSV control parameters for an instance of the SplFileObject class. These parameters determine how CSV data is parsed and processed from the file.

Syntax

public SplFileObject::getCsvControl(): array

Parameter

This function does not have any parameters.

Return Value

The SplFileObject::getCsvControl() returns three elements delimiter, enclosure, and escape character.

Program 1: The following program demonstrates the SplFileObject::getCsvControl() Function.

PHP




<?php
$file = new SplFileObject("./data.txt", "r");
$csvControl = $file->getCsvControl();
  
// $csvControl is an array 
// containing CSV control parameters
list($delimiter, $enclosure, $escape) = $csvControl;
echo "Delimiter: $delimiter\n";
echo "Enclosure: $enclosure\n";
echo "Escape: $escape\n";
?>


Output:

Delimiter: ,
Enclosure: "
Escape: \

Program 2: The following program demonstrates the SplFileObject::getCsvControl() Function.

PHP




<?php
$file = new SplFileObject("data.csv", "r");
$csvControl = $file->getCsvControl();
  
list($delimiter, $enclosure, $escape) = $csvControl;
  
echo "Original Delimiter: $delimiter\n";
echo "Enclosure: $enclosure\n";
echo "Escape: $escape\n";
  
if ($delimiter !== ",") {
    
    // Change delimiter to a semicolon
    $newDelimiter = ";";
    $file->setCsvControl($newDelimiter, $enclosure, $escape);
  
    echo "Delimiter changed to: $newDelimiter\n";
}
  
// Rewind the file to the beginning
$file->rewind();
  
// Read and display the modified CSV data
foreach ($file as $row) {
    $data = $file->fgetcsv();
    if ($data !== false) {
        echo implode(", ", $data) . "\n";
    }
}
  
?>


Note: Save this file name as “data.csv

1,"Eldon Base for stackable storage shelf, platinum",Muhammed MacIntyre,3,-213.25,38.94,35,Nunavut,Storage & Organization,0.8
2,"1.7 Cubic Foot Compact ""Cube"" Office Refrigerators",Barry French,293,457.81,208.16,68.02,Nunavut,Appliances,0.58
3,"Cardinal Slant-D� Ring Binder, Heavy Gauge Vinyl",Barry French,293,46.71,8.69,2.99,Nunavut,Binders and Binder Accessories,0.39
4,R380,Clay Rozendal,483,1198.97,195.99,3.99,Nunavut,Telephones and Communication,0.58
5,Holmes HEPA Air Purifier,Carlos Soltero,515,30.94,21.78,5.94,Nunavut,Appliances,0.5
6,G.E. Longer-Life Indoor Recessed Floodlight Bulbs,Carlos Soltero,515,4.43,6.64,4.95,Nunavut,Office Furnishings,0.37
7,"Angle-D Binders with Locking Rings, Label Holders",Carl Jackson,613,-54.04,7.3,7.72,Nunavut,Binders and Binder Accessories,0.38
8,"SAFCO Mobile Desk Side File, Wire Frame",Carl Jackson,613,127.70,42.76,6.22,Nunavut,Storage & Organization,
9,"SAFCO Commercial Wire Shelving, Black",Monica Federle,643,-695.26,138.14,35,Nunavut,Storage & Organization,
10,Xerox 198,Dorothy Badders,678,-226.36,4.98,8.33,Nunavut,Paper,0.38

Output:

Original Delimiter: ,
Enclosure: "
Escape: \
2, 1.7 Cubic Foot Compact "Cube" Office Refrigerators, Barry French, 293, 457.81, 208.16, 68.02, Nunavut, Appliances, 0.58
4, R380, Clay Rozendal, 483, 1198.97, 195.99, 3.99, Nunavut, Telephones and Communication, 0.58
6, G.E. Longer-Life Indoor Recessed Floodlight Bulbs, Carlos Soltero, 515, 4.43, 6.64, 4.95, Nunavut, Office Furnishings, 0.37
8, SAFCO Mobile Desk Side File, Wire Frame, Carl Jackson, 613, 127.70, 42.76, 6.22, Nunavut, Storage & Organization, 
10, Xerox 198, Dorothy Badders, 678, -226.36, 4.98, 8.33, Nunavut, Paper, 0.38

Reference: https://www.php.net/manual/en/splfileobject.getcsvcontrol.php



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads