(PHP 5 >= 5.2.0, PHP 7, PHP 8)
SplFileObject::setCsvControl — Set the delimiter, enclosure and escape character for CSV
$separator
= ",", string $enclosure
= "\"", string $escape
= "\\"): voidSets the delimiter, enclosure and escape character for parsing CSV fields.
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 carattereescape
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 carattereenclosure
, il carattereescape
non ha significato speciale; non è nemmeno destinato ad effettuare l'escape di se stesso.
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.
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.
Nessun valore viene restituito.
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.
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.
|
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 ?>