php8.5
Home/ Manual/ spl / splfileobject/ SplFileObject::setCsvControl

SplFileObject::setCsvControl

PHP function Edit on GitHub ✎

(PHP 5 >= 5.2.0, PHP 7, PHP 8)

Set the delimiter, enclosure and escape character for CSV

Description

SplFileObject::setCsvControl(string $separator = ",", string $enclosure = "\"", string $escape = "\\"): void

Sets the delimiter, enclosure and escape character for parsing CSV fields.

Parameters

Escape-parameter

Return Values

Void

Changelog

VersionDescription
7.4.0The escape parameter now also accepts an empty string to disable the proprietary escape mechanism.

Examples

SplFileObject::setCsvControl example

php
<?php
$file = new SplFileObject("data.csv");
$file->setFlags(SplFileObject::READ_CSV);
$file->setCsvControl('|');
foreach ($file as $row) {
    list ($fruit, $quantity) = $row;
    // Do something with values
}
?>

Contents of data.csv

txt
<?php
apples|20
bananas|14
cherries|87
?>

See Also

Source: reference/spl/splfileobject/setcsvcontrol.xml · from the official PHP manual (php/doc-en)