Remove line break after each INSERT

3

I have a textarea in my code, where the user will enter access vouchers separated by ENTER.

My PHP code treats the typed values as follows:

$voucher = explode("\n",$voucher);
      foreach ($voucher as &$varray)
      {
        $gravar = mysqli_query("INSERT INTO 'vouchers' (voucher, tempo) VALUES ('$varray','$tempoBD')");
      }

Where: $voucher is the value of the textarea received via $_POST and $tempoBD is the value that the user selects converted in the format 00:00:00 .

The problem is: The voucher field is saving the value of the line + line break ( \n )!

What correction in my code do I have to remove the line break in the voucher field with each record inserted in the table?

Thank you!

    
asked by anonymous 19.09.2017 / 15:16

2 answers

2

Use the strip_tags next to the trim, strip_tags will remove any html or php code from your file and the trim will remove the start and end spaces.

$voucher = explode("\n",$voucher);
foreach ($voucher as &$varray){
    $formata = strip_tags(trim($varray));
    $gravar = mysqli_query("INSERT INTO 'vouchers' (voucher, tempo) VALUES ('".$formata."','$tempoBD')");
}
    
19.09.2017 / 15:18
3

Use trim() to remove line breaks.

$voucher = explode("\n",$voucher);
foreach ($voucher as &$varray){
    $gravar = mysqli_query("INSERT INTO 'vouchers' (voucher, tempo) VALUES ('".trim($varray)."','$tempoBD')");
}
    
19.09.2017 / 15:19