search

Home  >  Q&A  >  body text

Create a csv file containing a large number of txt files

I'm trying to read a large txt file and save the first line as header and the rest of the text as content, then export to a CSV file.

I'm creating an id for a CSV that is incremented by iteration, but when I get an error that I can't see in the iteration, because when it saves the contents of the array, it adds the last contents to the value.

I need to create a CSV with 3 "columns" named id, titulo and contenido and save the information in an array by each file. One txt file, one array iteration.

Sorry for my bad English.

This is my code:

<?php
/* Cogemos todos los archivos txt de la carpeta archivos del servidor */
$files = glob("archivos/*.txt");
/* Creamos el array para guardar los datos y metemos la primera línea que es el nombre de los campos a importar */
$datosparacsv=array(array("ID","titulo","contenido"));
/* Creamos el id que tendrá cada campo del array para después poder importar */
$id = 0;
/* Recorremos cada archivo para coger los datos */
foreach($files as $file) {
    /* Sacamos el título de la primera línea del archivo txt */
    $titulo = trim(fgets(fopen($file, 'r')));
    /* Sacamos el resto del contenido pero quitamos la primera linea con el condicional if*/
    $archivo = file($file);
    foreach ($archivo as $num=>$line){
        if ($num==0) {
            continue;
        }
        else{
            $contenido .= $line."\n";
        }
    }
    /* Añadimos el contenido extraido al array para luego pasarlo a CSV */
    array_push($datosparacsv, array($id,$titulo,$contenido));
    /* Sumamos uno al id para que sea único */
    $id;
}
$delimitador = ','; //parameter for fputcsv
$enclosure = '"'; //parameter for fputcsv
//convert array to csv
$archivocsv = fopen('entradas.csv', 'w ');
foreach ($datosparacsv as $data_line) {
    fputcsv($archivocsv, $data_line, $delimitador, $enclosure);
}

$data_read="";
rewind($archivocsv);
//read CSV
while (!feof($archivocsv)) {
    $data_read .= fread($archivocsv, 8192); // will return a string of all data separated by commas.
}
fclose($archivocsv);
echo $data_read;

Example of file to read.

File1.txt

Titulo 1
texto 1

File2.txt

Titulo 2
texto 2

CSV

id, titulo, contenido, 0, Titulo 1, texto 1, 1, Titulo 2, texto 2

Thank you very much, friends.

P粉287254588P粉287254588301 days ago461

reply all(2)I'll reply

  • P粉198670603

    P粉1986706032024-02-27 11:15:53

    I use this form because I can format my answers better.

    I need the entire contents of the file minus the first line to be in the $contenido column.

    Now, using your code works fine, but if the same file has more than one line after the content, it will use each line as a new line for the result.

    For example, I am using this file now

    File1.txt

    Titulo 1
    texto 1, texto 1
    
    Some more text in file 1

    File2.txt

    Titulo 2
    texto 2, texto 2, texto 2, texto 2, texto 2, texto 2
    
    Some text 2 of the same archive

    This will generate this entradas.csv

    ID,titulo,contenido
    0,"Titulo 1","texto 1,texto 1"
    1,"Titulo 1",
    2,"Titulo 1","Some more text in file 1"
    3,"Titulo 2","texto 2, texto 2, texto 2, texto 2, texto 2, texto 2"
    4,"Titulo 2",
    5,"Titulo 2","Some text 2 of the same archive"

    But I need this:

    ID,titulo,contenido
    0,"Titulo 1","texto 1,texto 1
    
    Some more text in file 1"
    1,"Titulo 2","texto 2, texto 2, texto 2, texto 2, texto 2, texto 2
    
    Some text 2 of the same archive"

    It is very important that the content saves all spaces and \n in the txt file because the txt file is a blog post.

    Example of a file .txt

    ¿ Como puedo comer galletas?<-- Title
    
    Las galletas se comen con la boca, poco a poco masticando.
    
    

    ¿ Cuántos sabores de galletas hay?

    All text after the title must be kept on the same line to save \n and everything.

    A file has only one row in CSV.

    Thank you very much and I'm sorry for my English.

    reply
    0
  • P粉548512637

    P粉5485126372024-02-27 11:12:31

    $contenido on line 19 is undefined, it attempts to connect a non-existent variable to .=. The $contenido variable is also not required since each archive line is defined in $datosparacsv.

    There is no need to define $delimitador and $enclosure, because the defined values ​​are also default values.

    This is correct PHP code with the expected CSV output and comments explaining each modified line.

    It also preserves new lines and spaces in the content as needed.

    File/1.txt

    Titulo 1
    texto 1

    File/2.txt

    Titulo 2
    texto 2
    
    texto3
    
    
    
    texto4

    This will save entradas.csv with this data.

    ID,titulo,contenido
    0,"Titulo 1","Titulo 1"
    1,"Titulo 2","texto 2
    
    texto3
    
    
    
    texto4"

    reply
    0
  • Cancelreply