Frage

  1. Ich bin ein Skript erstellen, die eine CSV-Datei lesen und es auf einem Textfeld angezeigt werden fgetcsv verwendet wird.

      $handle = @fopen($filePath, "r");
    
      if ($handle)
      {
         while (($buffer = fgetcsv($handle, 1000,",")) !== false) 
         {
              foreach($buffer as $buff){
                 echo $buff."\n";
              }
    
         }
      }      
    
  2. Das Format der CSV ist

    "line1-content1", "line1-content2"
    "Line2-content1", "line2-content2"

  3. Mit fgetcsv, der Inhalt wird in dem Textbereich ohne doppelte Anführungszeichen und Komma angezeigt werden soll. Kann ich es so formatiert werden, dass sie auch die duoble Zitate und Komma angezeigt werden?

  4. Dann beim Speichern sie mit fputcsv

    $file_to_load = $_GET['filepath'];
    
    $filePath = $dir.$file_to_load;
    
    $trans = trim($_POST['txtarea']);
    $keyarr = split("\n",$trans);
    
    $fp = fopen($filePath, 'w');
    foreach (array ($keyarr) as $fields) {
      fputcsv($fp, $fields);
    }
    
    fclose($fp);
    
  5. Suchen Sie auf der CSV-Datei gespeichert es die csv aber zeigt es wie folgt

    "line1-content1
    "" Line1-content2
    "" Line2-content1
    “, "Line2-content2"

  6. Sie trennen die "line1-content1" und "line1-content2" in zwei Zeilen und legen ein Komma nach dem Ende jeder Zeile.

  7. Jetzt möchte ich die Formatierung von # 2 halten. Wie werde ich es Code?

Können Sie mir in die richtige Richtung lenken? Dank!

War es hilfreich?

Lösung

Sounds like you want to display the actual raw CSV text, not the parsed data within the CSV. Instead of using fgetcsv(), just use fgets() and you'll get the text line without any parsing, preserving the quotes and commas.

As for fputcsv, it's going to write out what you pass into it, so make sure that whatever's coming back from the form is cleaned up (e.g. extra line breaks stripped out).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top