简体   繁体   English

使用nl2br仅插入一次换行符,而不是在每个新行之前插入换行符

[英]using nl2br to insert a line break only once rather than before each new line

I have a string and i am using nl2br to insert line breaks. 我有一个字符串,我正在使用nl2br插入换行符。

how can i make it only insert one new line break before all newlines rather than a line break before all new lines 我如何才能使其仅在所有换行符之前插入一个新的换行符,而不是在所有新行之前插入换行符

You could use preg_replace() afterwards to match all repeating BR's and replace with a single one 之后,您可以使用preg_replace()来匹配所有重复的BR并替换为一个

$pattern = "/(\<br\/\>)+/i";
$replacement = "<br/>";
$subject = "hi<br/><br/><br/>how are<br/>you";
$limit = -1;
$count = 0;
$result = preg_replace ($pattern, $replacement, $subject, $limit, $count);

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

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