Php nl2br中只允许一个br

Php nl2br中只允许一个br,php,Php,我让我的网站成员在textarea上发布一些关于他们的信息。我使用functionnl2br使它更漂亮,如下所示: 但问题是。我不想允许人们在文本中使用多个enter(br),那么我能做什么呢?您可以做: $text_of_area = nl2br(str_replace("\r\n", "\n", $_POST['text_area_name'])); 为什么不在呼叫nl2br之前更换多条新线路 如果您想让他们在帖子中只使用一行新词: $firstPos = strpos($text,

我让我的网站成员在textarea上发布一些关于他们的信息。我使用function
nl2br
使它更漂亮,如下所示:

但问题是。我不想允许人们在文本中使用多个enter(br),那么我能做什么呢?

您可以做:

$text_of_area = nl2br(str_replace("\r\n", "\n", $_POST['text_area_name']));

为什么不在呼叫nl2br之前更换多条新线路

如果您想让他们在帖子中只使用一行新词:

$firstPos = strpos($text, "\n");
if ($firstPos !== false) {
    $text = substr_replace(array("\r","\n"),'', $text, $firstPos + 1);
}
$text = nl2br($text);
如果您想让他们只使用一个连续的新行(允许
foo\nbar\nbaz
):

$firstPos = strpos($text, "\n");
if ($firstPos !== false) {
    $text = substr_replace(array("\r","\n"),'', $text, $firstPos + 1);
}
$text = nl2br($text);
$text = preg_replace('#[\r\n]+#', "\n", $text);
$text = nl2br($text);