Problem with php spacing in file handling

1

I explain the program

  • Create a .txt file based on an array.
  • Pressing the "read" button reads a file using the read_file function.
  • If the "sort" button is pressed, a text file is searched, in this case the one that was created before and converted to an arrangement to be passed through the quicksort function that will order it and then, again, it will save it to a new file with the lines in order.
  • Problem:

    When the second file is created, sort the elements well but leave a line of space between them , which I would like to change.

    Example:

    Desired result:

    XXXXX 
    XXXXX
    XXXXX
    

    Result Obtained

    XXXXX
    
    XXXXX
    
    XXXXX
    

    I leave the code

    <?php 
    
    $n_archivo = 'nuevo_archivo.txt';
    //array para crear un nuevo archivo
    $ordenar = array('carro','auto','01','0001','zorro','alto','piensa','zorro');
    //creando nuevo archivo
    nuevo_archivo($ordenar,'nuevo_archivo.txt');
    
    if (isset($_POST['leer']))//si fue presionado leer ...
    {
        leer_archivo($n_archivo,FALSE);
    }
    elseif (isset($_POST['ordenar']))//si fue presionado ordenar ...
    {
    
        nuevo_archivo(quicksort(leer_archivo($n_archivo,TRUE)),'ordenado.txt');
        leer_archivo('ordenado.txt',FALSE);
    
    }
    
    echo <<<_END
    <html>
        <head>
            <title>Examen quicksort Leonel Becerra</title>
        </head>
    
        <body>
            <form method="post" action="quicksort_examen.php">
                Leer Archivo    <input type="submit" name="leer" value="leer">
                Ordenar archivo <input type="submit" name="ordenar" value="ordenar">
            </form>
        </body>
    
    </html>
    
    _END;
    
    //lee un archivo, si $array es falso solo lo imprime, si es verdadero crea un array de elementos con cada linea del archivo
    function leer_archivo($nombre,$array)
    {
        $fh = fopen("$nombre",'r')or die ($php_errormsg);
    
        if(!$array)
        {
    
            while(!feof($fh))
            {
                echo fgets($fh)."<br/>";
            }
    
            fclose($fh) or die ($php_errormsg);
        }
        else
        {
            $arreglo;
            $i = 0;
            while(!feof($fh))
            {                       
                $arreglo[$i] = fgets($fh);
                $i++;
            }
    
            fclose($fh) or die ($php_errormsg);
    
            return $arreglo;
    
        }
    
    }
    
    //ordenamiento por quicksort
    function quicksort($array)
    {
        $tamaño = count($array); //encontrando el tamaño del arreglo
    
        if($tamaño <= 1 ) //caso base
        {
            return($array);
        }
        else
        {
            $pivot = $array[0]; //asignando pivot
    
            $menor = array(); //array para menores que pivot
            $mayor = array(); //array para mayores que pivot
    
            for($i = 1; $i < count($array); $i++) //poniendo cada elemento en su lugar comparandolos con el pivot
            {
                if($array[$i] < $pivot)
                {
                    $menor[] = $array[$i];
                }
                else
                {
                    $mayor[] = $array[$i];
                }
            }
    
            return array_merge(quicksort($menor), array($pivot), quicksort($mayor)); /*une arreglos $menor con $pivot con $mayor*/
        }
    }
    
    //crea un nuevo archivo tomando como parametro un arreglo
    function nuevo_archivo($array,$nombre)
    {
        $fh = fopen("$nombre", 'w') or die ($php_errormsg);
    
        for($i = 0; $i < count($array); $i++)
        {
            $temp = $array[$i];
            $temp = sanitizar_variable($temp);
            $temp = $temp."\r\n";
            fwrite($fh,$temp) or die ($php_errormsg);
        }
    
        fclose($fh);
    
    }
    
    //usada para quitar slashes, codigo html y tags html
    function sanitizar_variable($var)
    {
        $var = stripslashes($var);
        $var = htmlentities($var);
        $var = strip_tags($var);
    
        return $var;
    }
    
    //Imprime el arreglo, usado para debugging
    function imprimir_arreglo($array)
    {
        for($i = 0; $i < count($array); $i++)
        {
            echo "$array[$i]";
        }
    }
    ?>
    
        
    asked by Leo 23.02.2016 в 02:28
    source

    1 answer

    3

    The problem is that the lines YA have the line break, when you add another line with ."\r\n" they are duplicated and you see the double jumps. This happens because fgets reads the entire line, including the \r\n\ of the end.

    It would be best to delete the line break when you read the file. Modifying this piece of function leer_archivo and using trim .

    $arreglo[$i] = trim(fgets($fh)); 
    
        
    answered by 23.02.2016 / 02:42
    source