str_getcsv

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

str_getcsv Parse a CSV string into an array

Descrizione

str_getcsv(
    string $string,
    string $separator = ",",
    string $enclosure = "\"",
    string $escape = "\\"
): array

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 su en_US.UTF-8.

Elenco dei parametri

string

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

Returns an indexed array containing the fields read.

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

Esempi

Example #1 str_getcsv() example

<?php

$string
= 'PHP,Java,Python,Kotlin,Swift';
$data = str_getcsv($string);

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

Attenzione

On an empty string this function returns the value [null] instead of an empty array.

<?php

$string
= '';
$data = str_getcsv($string);

var_dump($data);
?>

Il precedente esempio visualizzerà:

array(1) {
  [0]=>
  NULL
}

Vedere anche: