Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 文本区域,nl2br,大量换行_Php_Html - Fatal编程技术网

Php 文本区域,nl2br,大量换行

Php 文本区域,nl2br,大量换行,php,html,Php,Html,我有一个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,用户在其中输入文本(根据需要返回多少个),然后我获取该值,将其插入数据库,然后用数据库中的值更新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>

这是我的html。我遇到的问题是,在提交值并将其插入数据库工作时,当它填充textarea的值时,换行符的数量会加倍。 所以如果他们打字

嘿,断线 福巴

它将使textarea的值

哎 断线 断线 福巴

函数nl2br_lose($string){ 返回str_replace('
',' ;',str_replace('
',' ;',str_replace('
',' ;',str#replace('
',' ;',$string)); } 是我用来将nl2br转换为textarea“returns”的函数。但是,如果我从返回中取出nl2br_lose,它只有一个
,因此问题一定存在。今天的大部分时间里,我都在为这件事烦恼


提前谢谢

nl2br在换行符前面添加br,但不删除换行符。当您将该输出包装到nl2br_lose中时,需要:

blahblah<br>\nblahblah

您只需将
br
匹配项替换为零,因为nl2br不会删除空白

return str_replace(array(
        '<br/>',
        '<br />',
        '<br>',
        '<br >'
    ), '', $string);
返回str\u替换(数组(
“
”, “
”, “
”, “
” ),“”,$string);
另外,您可能希望将nl2br lose中的名称从
nl2br lose
更改为
br2nl
:)

`return preg_replace("/<br\s?\/?>/", '', $string); //removes <br>, <br/>, <br />, <br >`
return str_replace(array(
        '<br/>',
        '<br />',
        '<br>',
        '<br >'
    ), '', $string);
`return preg_replace("/<br\s?\/?>/", '', $string); //removes <br>, <br/>, <br />, <br >`
function stripnl2br($string) {return str_replace("\n", '', nl2br($string));}