Textarea characters break line when submitting to database

1

I have a form with a field SELECT and when submitting the text in the database, it breaks line, even copied and pasted directly from the table record in the notepad and is even broken, in HTML the SELECT It's like this:

<textarea id="descricao" name="descricao" wrap="physical" class="form-control input-lg" placeholder="Insira a descrição relacionada..." rows="22" draggable="true"></select>

Before being inserted into the database, the value of TEXTAREA goes through a validation:

<?php
if( !function_exists('filtra_var') ){
    function filtra_var($var) {
        $var = trim($var);
        $var = strip_tags($var);
        $var = addslashes($var);
        return $var;
    }
}

$descricao = $_POST['desricao']
$desricao = filtra_var( $descricao );

/* INSERT IN DB */
?>

The following text inserts it like this, without breaking:

  

What is Lorem Ipsum?

     

Lorem Ipsum is simply dummy text of the printing and typesetting   industry. Lorem Ipsum has been the industry's standard dummy text ever   since the 1500s, when an unknown printer took a galley of type and   scrambled it to make a type specimen book.

Retrieving the table record with PHP:

<?php
$descricao = stripslashes( $row['descricao'] ); // $row == mysqli_fetch_array( $query_sql, MYSQLI_ASSOC); 

$descricao = nl2br ( $descricao );

echo $descricao;
?>

The text returned on the screen is broken:

  

What is Lorem Ipsum?

     

Lorem Ipsum is simply dummy text of the printing

     

and typesetting industry. Lorem Ipsum has been the

     

industry's standard dummy text ever since the 1500s,

     

When an unknown printer took a galley of type

     

and scrambled it to make a specimen type book.

Well, there is no line break, that is, <br> , in addition to "What is Lorem Ipsum?", and the text is broken, should be in the database like this:

  

What is Lorem Ipsum?

     

Lorem Ipsum is simply dummy text of the printing   and typesetting industry. Lorem Ipsum has been the   industry's standard dummy text ever since the 1500s,   when an unknown printer took a galley of type   and scrambled it to make a type specimen book.

    
asked by anonymous 21.09.2018 / 16:02

1 answer

0

I solved the problem, I increased the code that validates the value before it was inserted into the database.

<?php
if( !function_exists('filtra_var') ){
    function filtra_var($var) {
        $var = trim($var);
        $var = strip_tags($var);
        $var = addslashes($var);
        return $var;
    }
}

$descricao = $_POST['desricao']
$desricao = filtra_var( $descricao );
$descricao = nl2br( $descricao, false );

/* INSERT IN DB */
?>

So the text is sent with the line break and instead of \n the <br> is inserted in the database.

And the value impression looks like this:

<?php
$descricao = stripslashes( $row['descricao'] ); // $row == mysqli_fetch_array( $query_sql, MYSQLI_ASSOC); 

$descricao = stripslashes( $descricao );

echo $descricao;
?>
    
21.09.2018 / 16:50