php / php/php-src

str_getcsv won't work correctly in some special cases

Aperta
#10,566 6 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Bug Extension: standard Status: Needs Triage
Lingua principale
C
Stelle
40.4k
Fork
8.2k
Merge medio
2g 13h
PR unite (30g)
96

Descrizione

Description

The function should not rebuild the string-annotation in the CSV-String by using the commas, if str_getcsv is used in the first step of the small algorithm.
The following code is motivated by the example of durik at 3ilab dot net : He mentioned: "...
Why not use explode() instead of str_getcsv() to parse rows? Because explode() would not treat possible enclosured parts of string or escaped characters correctly."

<?php

$string =<<<DOMTEST
"PHP, Java","Python"
"PHP2, Java2","Python"
"PHP3, Java3","Python"
DOMTEST;

  $data = str_getcsv($string, "\n" );
        foreach ($data as &$row) {
            $row = str_getcsv($row, ',');
        }
var_dump($data);

Resulted in this output:

array(3) {
  [0]=>
  array(3) {
    [0]=>
    string(3) "PHP"
    [1]=>
    string(5) " Java"
    [2]=>
    string(6) "Python"
  }
  [1]=>
  array(3) {
    [0]=>
    string(4) "PHP2"
    [1]=>
    string(6) " Java2"
    [2]=>
    string(6) "Python"
  }
  [2]=>
  &array(3) {
    [0]=>
    string(4) "PHP3"
    [1]=>
    string(6) " Java3"
    [2]=>
    string(6) "Python"
  }
}

But I expected this output instead:

array(3) {
  [0]=>
  array(2) {
    [0]=>
    string(8) "PHP, Java"
    [1]=>
    string(6) "Python"
  }
  [1]=>
  array(2) {
    [0]=>
    string(10) "PHP2, Java2"
    [1]=>
    string(6) "Python"
  }
  [2]=>
  &array(2) {
    [0]=>
    string(10) "PHP3, Java3"
    [1]=>
    string(6) "Python"
  }
}
PHP Version

8.2.1

Operating System

No response

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia riproducendo l'esempio PHP fornito usando str_getcsv con delimitatori di nuova riga e virgola, quindi esamina il comportamento esistente della funzione per le virgole tra virgolette e i caratteri con escape. L'issue non indica alcun file di implementazione né alcun test; il lavoro è considerato completato quando il comportamento previsto dell'analisi annidata è stabilito e coperto da test di regressione.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
php
Ambito
backend-api-design
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.