Line breaks displayed as \ r \ n in textarea

I am trying to display data in textarea that is retrieved from tables that I sent through another form. The problem occurs when entering a new line.

The data displayed in the text box is equal

Lin1 \ r \ nlin2

he should be like

lin1
lin2

I tried nl2br but it does not work properly. How can I make things optimized. Thanks

+4
source share
7 answers

I hope that saves you. str_replace

<?php
$str='lin1\r\nlin2';
$str=str_replace('\r\n','<br>',$str);
echo $str;

OUTPUT:

lin1
lin2
+4
source

This problem can be solved by using the stripcslashes()output of your data.

Please note that the method above is different from stripslashes()which does not work in this case.

nl2br, .

+5

, ln2br str_replace.

.

, escape- mysql . , . , .

+4
<?php echo str_replace('\r\n', "\r\n", $text_with_line_breaks); ?>

. . .

.

+2

\r\n, "\ r\n". .

+1

insert/update

You can solve this problem in your case by doing the following

<?php
$str = 'lin1\r\nlin2';
$solved_str = str_replace(array("\\r","\\n"), array("\r","\n"), $str);

var_dump($str,$solved_str);

But you need to check the insert / update statement on the escape characters of quotes

0
source

For non-textarea use this function

function escapeNonTextarea($string){

    $string=str_replace(array('\n','\r\n','\r'),array("<br>","<br","<br>"),$string);
    return $string;
}

For text area use this function

function escapeTextarea($string){

    $string=str_replace(array('\n','\r\n','\r'),array("\n","\r\n","\r"),$string);

    return $string;
}

call the appropriate function and pass an argument

0
source

All Articles