我有以下文本,我想<br>在每个段落之间添加一个标记.并删除所有换行符.我将如何在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.
Run Code Online (Sandbox Code Playgroud)
会成为这个 -
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.
Run Code Online (Sandbox Code Playgroud)
注意:忽略任何字母的突出显示.
好吧,似乎你考虑一个段落分隔符,一个空行.所以最简单的解决方案似乎如下:
$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;
}
Run Code Online (Sandbox Code Playgroud)
我认为这解决了你的问题.$textResult会得到你的结果
这也应该有效:(虽然过于简单化)
$string = str_replace("\n\n", "<br />", $string);
$string = str_replace("\n", "", $string);
Run Code Online (Sandbox Code Playgroud)
它经过测试.