gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-10-31 23:56:34 35 4
gpt4 key购买 nike

我有以下文本,我想为其添加 <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.

会变成这样——

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.

注意:忽略任何字母的突出显示。

最佳答案

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

$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;
}

我认为这可以解决您的问题。 $textResult 会有你的结果

关于php - 在 PHP 中删除换行符并添加 BR 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2813071/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com