SplFileObject::setCsvControl

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

SplFileObject::setCsvControlSet the delimiter, enclosure and escape character for CSV

Descrizione

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

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

Elenco dei parametri

separator

Il parametro separator imposta il separatore di campo. Deve essere un carattere a byte singolo.

enclosure

Il parametro enclosure imposta il carattere di chiusura del campo. Deve essere un carattere a byte singolo.

escape

Il parametro escape imposta il carattere di escape. Deve essere un carattere a byte singolo o la stringa vuota. La stringa vuota ("") disabilita il meccanismo di escape proprietario.

Nota: Di solito, per effettuare l'escape di un carattere enclosure all'interno di un campo lo si duplica; tuttavia, il carattere escape può essere usato come alternativa. Pertanto, con i valori predefiniti del parametro "" e \" hanno lo stesso significato. Oltre a consentire di effettuare l'escape del carattere enclosure, il carattere escape non ha significato speciale; non è nemmeno destinato ad effettuare l'escape di se stesso.

Avviso

A partire da PHP 8.4.0, dipendere dal valore predefinito di escape è deprecato. Deve essere fornito esplicitamente, sia per posizione che mediante l'uso dei named arguments.

Avviso

When escape is set to anything other than an empty string ("") it can result in CSV that is not compliant with » RFC 4180 or unable to survive a roundtrip through the PHP CSV functions. The default for escape is "\\" so it is recommended to set it to the empty string explicitly. The default value will change in a future version of PHP, no earlier than PHP 9.0.

Valori restituiti

Nessun valore viene restituito.

Errori/Eccezioni

Genera un ValueError se separator o enclosure non sono lunghi un byte.

Genera un ValueError se escape non è lungo un byte o è una stringa vuota.

Log delle modifiche

Versione Descrizione
8.4.0 L'affidamento sul valore predefinito di escape è ora deprecato.
7.4.0 The escape parameter now also accepts an empty string to disable the proprietary escape mechanism.

Esempi

Example #1 SplFileObject::setCsvControl() example

<?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

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

Vedere anche:

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top