在PHP中删除换行符并添加BR标记

在PHP中删除换行符并添加BR标记,php,html,Php,Html,我有以下文本,我想在每个段落之间添加一个标记。并移除所有换行符。在PHP中如何实现这一点?谢谢 所以这个—— This is some text for which I would like to remove the line breaks. And I would also like to place a b> tag after every paragraph. Here is one more paragraph. 会变成这样- This is some text f

我有以下文本,我想在每个段落之间添加一个

标记。并移除所有换行符。在PHP中如何实现这一点?谢谢

所以这个——

This is some text
for which I would
like to remove 
the line breaks.

And I would also 
like to place
a b>  tag after 
every paragraph.

Here is one more
paragraph.
会变成这样-

This is some text for which I would like to remove the line breaks.<br/> And I would also like to place a br tag after every paragraph. <br> Here is one more paragraph.
这是一些我想删除换行符的文本。
并且我还想在每个段落后面放置一个br标记<这里还有一段。
注意:忽略任何字母的突出显示。

echo str\u replace(数组(“\n\n”,““\n”)、数组(“
”,“”“),$subject);
echo str_replace(array("\n\n", "\n"), array("<br/>", " "), $subject);
上面用

标记替换了双换行符,并将任何剩余的单换行符替换到一个空格中(以避免最初仅由换行符分隔的单词相互碰撞)


您是否需要满足CRLF(窗口)样式的换行符;这会稍微改变(虽然不是非常)改变方法。

嗯,看起来你考虑段落分隔符,一个空行。因此,最简单的解决方案似乎是:

$text  = str_replace( "\r", "", $text ); // this removes the unwanted \r
$lines = explode( "\n", $text ); // split text into lines.
$textResult = "";
foreach( $lines AS $line )
{
   if( trim( $line ) == "" ) $textResult .= "<br />";
   $textResult .= " " . $line;
}
$text=str\u replace(“\r”,”,$text);//这将删除不需要的\r\n
$lines=分解(“\n”,$text);//将文本拆分为行。
$textResult=“”;
foreach($line作为$line)
{
如果(修剪($line)==”)$textResult.=“
”; $textResult.=''.$line; }
我认为这解决了你的问题
$textResult
将使您的结果也能运行:(尽管过于简单)

$string=str\u replace(“\n\n”,“
,$string”); $string=str\u replace(“\n”,”,$string);
它已经测试过了。

这对我来说很有用


$string=ereg\u replace(“\n”,“
,$string”)

要么我遗漏了什么,要么没有人给出最简单的解决方案——内置函数:


请记住,如果您使用纯字符串(不是变量)作为
nl2br
的参数,则必须使用双引号,否则您的控制字符,如
\n
\r
将无法展开。

您是否有意使用两种不同样式的
br
标记?这似乎也是正确的,但我想您无法判断空行上是否有空格,或者是否有空格\r\nchars@Gabriel:当然,我们只能用提问者给我们的东西做到最好。+1啊,最后,有点理智的人!我还没有测试,所以它更好的工作,否则我会回来的!
$string = str_replace("\n\n", "<br />", $string);
$string = str_replace("\n", "", $string);
echo nl2br($string);