Text area, nl2br, line breaks galore

I have a text box where users enter text (with the number of returns as they want), and I take this value, paste it into the database and update the textarea value using one in the database.

<textarea maxlength="500" cols="110" name="description" rows="15"><?php if(isset($newDesc)) echo snl2br_lose(nl2br($newDesc)); else echo nl2br_lose(nl2br($user->desc));?></textarea>

is my html. The problem I am facing is that when sending the value and pasting it into the database, it doubles the number of rows when it fills the value of the text field. Therefore, if they print

Hi, line break foobar

he will make the textarea value

Hey line break line break foobar

function nl2br_lose($string) { 
     return str_replace('<br/>', '&#013;', str_replace('<br />', '&#013;',str_replace('<br>', '&#013;',str_replace('<br >', '&#013;',$string)))); 
 } 

- , , nl2br textarea "". , nl2br_lose ,
, . .

!

+3
3

nl2br_lose:

`return preg_replace("/<br\s?\/?>/", '', $string); //removes <br>, <br/>, <br />, <br >`

, . - nl2br:

function stripnl2br($string) {return str_replace("\n", '', nl2br($string));}
+3

br , nl2br .

return str_replace(array(
        '<br/>',
        '<br />',
        '<br>',
        '<br >'
    ), '', $string);

P.S. , nl2br_lose br2nl:)

+2

nl2br br , . nl2br_lose, :

blahblah<br>\nblahblah

nl2br

blahblah\n\nblahblah
0
source

Source: https://habr.com/ru/post/1779905/


All Articles