简体   繁体   English

文本区域,nl2br,线条丰富

[英]Text Area, nl2br, line breaks galore

I have a textarea, where the users enters text (with as many returns as they want), and I take that value, insert it into a database, and then update the textarea's value with the one in the database. 我有一个textarea,用户输入文本( 返回任意数量),然后我将该值,插入数据库,然后用数据库中的值更新textarea的值。

<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. 是我的HTML。 The issue i'm having is, whilst submitting the value and inserting it into the database works, it doubles the amount of linebreaks when it fills the value of the textarea. 我遇到的问题是,在提交值并将其插入数据库工作时,它会在填充textarea的值时将换行量增加一倍。 So if they type 所以,如果他们打字

Hey line break Foobar 换线 Foobar

it will make the textarea's value 它将使textarea的价值

Hey line break line break Foobar 线断线 Foobar的突破

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

is the function i'm using to turn nl2br into textarea "returns". 是我用来将nl2br变成textarea“返回”的函数。 However, if I take out nl2br_lose from the return, it only has one 但是,如果我从返回中取出nl2br_lose,它只有一个
, so the issue must be there. 所以问题必须存在。 I've been having trouble with this for the better part of today. 在今天的大部分时间里,我一直遇到麻烦。

Thanks in advance! 提前致谢!

in nl2br_lose: 在nl2br_lose中:

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

of course, it exchanges readibility for simplicity. 当然,为了简单起见,它交换了可读性。 the other option is to write a function to call instead of nl2br: 另一个选择是写一个函数来调用而不是nl2br:

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

You need to just replace the br matches with nothing, because nl2br does not remove whitespace. 您只需要替换没有任何内容的br匹配,因为nl2br不会删除空格。

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

PS You might want to change the name from nl2br_lose to br2nl :) PS您可能想要将名称从nl2br_lose更改为br2nl :)

nl2br adds br's in front of newlines, but doesnt remove newlines. nl2br在换行符前添加br,但不删除换行符。 When you wrap that output in your nl2br_lose it takes: 当您在nl2br_lose中包装该输出时,它需要:

blahblah<br>\nblahblah

from nl2br and turns it into 来自nl2br并把它变成了

blahblah\n\nblahblah

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM