Delete PHP blank space

1

By php I generate and save a txt file that contains the data captured from a textfield, but when reading it from an application that I am creating it turns out that it has a blank space of more. How can I save my txt with the info lines I want without generating a blank space at the end?

PHP code to save data in TXT:

if ($_POST["txtopc1"]) 
    {
        $miopc1=$_POST["txtopc1"];
        if ($miopc1!="")
        { 
                $archivo='D:\letritastv\aplicacion\nivel_1\opcion1.txt';
                $file = fopen($archivo, 'a') or die (); 
                fwrite($file,$miopc1.PHP_EOL);
                fclose($file);
                echo "He recibido en el archivo.php: ".$_POST["txtopc1"];
        }
        else { $miopc1=""; }
    }
        else{echo "He recibido un campo vacio";}

Code to read my file in LUA:

 local opc1 = io.open("nivel_1/opcion1.txt","r")
 local mopc1= opc1:read("*all")
 objdib = objetos:new()
 objdib:dibujar(mopc1)


 function objetos:dibujar (mopc1)
    canvas:attrColor('red')
    canvas:attrFont('Tiresias','40','normal')
    canvas:drawText(70,100,mopc1)
    canvas:flush()
 end

The result of reading my txt file is a box that represents a blank space:

    
asked by Estefani_Sanchez 20.05.2017 в 19:36
source

3 answers

1

It's very simple if I understand your question well.

You have a <form> which sends information to PHP and you receive it in this line:

$miopc1=$_POST["txtopc1"];

Within this line there are 2 errors, do not clean the information (but if it is not relevant to you then ignore this) and the second does not check that there is information (likewise, ignore if it is not relevant).

But at the same time you want to remove the blanks while being something like:

frase[ ] (Understand the [] as the blank).

For this, there is a function in PHP which allows you to remove those huge blank spaces that could be:

The trim () function in PHP (link to documentation) allows you to remove those spaces in white, passing the text as an argument, leaving your line in the following way:

$miopc1=trim($_POST["txtopc1"]);

And with this it would be ready.

    
answered by 20.05.2017 в 19:50
1

The problem I think is in the fopen, since for each line break it presents problems with \ n, reading the documentation says the following

so then in your reading option fopen would be something like

$file = fopen($archivo, 'ab') or die (); 
 //acompañar el modo de escritura con el flag b

Source: link

    
answered by 20.05.2017 в 19:55
1

You can try this way saludes.

if ($_POST["txtopc1"]) 
    {
        $miopc1=$_POST["txtopc1"];
        if ($miopc1!="")
        { 
                $archivo='D:\letritastv\aplicacion\nivel_1\opcion1.txt';
                $file = fopen($archivo, 'a') or die (); 
                fwrite($file,PHP_EOL .$miopc1);
                fclose($file);
                echo "He recibido en el archivo.php: ".$_POST["txtopc1"];
        }
        else { $miopc1=""; }
    }
        else{echo "He recibido un campo vacio";}
    
answered by 30.09.2017 в 09:41