(PHP 5 >= 5.3.0, PHP 7, PHP 8)
str_getcsv — Parse a CSV string into an array
$string,$separator = ",",$enclosure = "\"",$escape = "\\"Parses a string input for fields in CSV format and returns an array containing the fields read.
Nota: Le impostazioni locali vengono prese in considerazione da questa funzione. Ad esempio, i dati codificati in determinati formati a byte singolo potrebbero essere analizzati in modo errato se
LC_CTYPEè impostato suen_US.UTF-8.
stringThe string to parse.
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
enclosureall'interno di un campo lo si duplica; tuttavia, il carattereescapepuò 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 carattereescapenon 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.
Returns an indexed array containing the fields read.
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.
|
| 8.4.0 |
Now throws a ValueError if
separator, enclosure,
or escape is invalid.
This mimics the behavior of fgetcsv() and
fputcsv().
|
| 8.3.0 | Viene restituita una stringa vuota invece di una stringa con un singolo byte null per l'ultimo campo se contiene solo una chiusura non terminata. |
| 7.4.0 |
The escape parameter now interprets an empty
string as signal to disable the proprietary escape mechanism. Formerly,
an empty string was treated like the default parameter value.
|
Example #1 str_getcsv() example
<?php
$string = 'PHP,Java,Python,Kotlin,Swift';
$data = str_getcsv($string, escape: '\\');
var_dump($data);
?>Il precedente esempio visualizzerà:
array(5) {
[0]=>
string(3) "PHP"
[1]=>
string(4) "Java"
[2]=>
string(6) "Python"
[3]=>
string(6) "Kotlin"
[4]=>
string(5) "Swift"
}
Example #2 str_getcsv() example with an empty string
On an empty string this function returns the value [null]
instead of an empty array.
<?php
$string = '';
$data = str_getcsv($string, escape: '\\');
var_dump($data);
?>Il precedente esempio visualizzerà:
array(1) {
[0]=>
NULL
}