多个<br /> <br> <br/> \ r \ n出现需要替换为<br />

所罗门·克洛森(Solomon Closson)

我需要更换的多次出现<br /><br><br/>\r\n\r\n只有1 <br />,但只有当它显示了起来,这样我可以段落分成数组,并修剪出的最后一个换行符br在字符串的结束标记...

字符串的示例可能如下:

This is paragraph 1<br>

This is paragraph 2<br /><br>



This is paragraph 3
This is paragraph 4


This is paragraph 5<br/>
This is paragraph 6


This is paragraph 7<br />This is paragraph 8

This is paragraph 9<br>

我尝试过的

$description = !empty($results['Description']) ? strip_tags(rtrim(str_replace(array("\n", "\r", "\r\n", "<br>", "<br/>", "<br />"), array("<br />"), $results['Description']), '<br />'), '<br><a>') : '';

$paragraphs = array_filter(explode("<br />", $description));

但这会修剪掉所有的换行符。我需要保持换行符在字符串中的位置,但只想做1个换行符并删除所有其他换行符。这该怎么做?

注意:我正在使用strip_tags,因为我只想字符串中允许<br><a>标记$description

拉斯克拉特

你可能想尝试先用线(到2005年底爆炸PHP_EOL),去掉一切,但<a>,然后array_filter()implode()trim()

$str    =   'This is paragraph 1<br>

This is paragraph 2<br /><br>



This is paragraph 3
This is paragraph 4


This is paragraph 5<br/>
This is paragraph 6


This is paragraph 7<br />';

$str    =   trim(implode('<br />'.PHP_EOL,array_filter(explode(PHP_EOL,strip_tags($str,'<a>')))));

echo $str;

给你:

This is paragraph 1<br />
This is paragraph 2<br />
This is paragraph 3<br />
This is paragraph 4<br />
This is paragraph 5<br />
This is paragraph 6<br />
This is paragraph 7

您的第二种情况:

$str    =   'This is paragraph 1<br>

This is paragraph 2<br /><br>



This is paragraph 3
This is paragraph 4


This is paragraph 5<br/>This is paragraph 5
This is paragraph 6


This is paragraph 7<br />';

# Explode on line breaks and remove empty lines
$exp    =   array_filter(explode(PHP_EOL,$str));
# Loop array and filter the lines
$new    =   array_map(function($v) {
                return strip_tags(trim($v,'<br>,<br />,<br/>'),'<a><br>');
            },$exp);
# Implode the array back
echo implode('<br />'.PHP_EOL,$new);

给你:

This is paragraph 1<br />
This is paragraph 2<br />
This is paragraph 3<br />
This is paragraph 4<br />
This is paragraph 5<br/>This is paragraph 5<br />
This is paragraph 6<br />
This is paragraph 7

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

之间的差异:<br/>与<br/> = 20

来自分类Dev

php mail () /r/n and <br> have no effect

来自分类Dev

php mail()/ r / n和<br>无效

来自分类Dev

用<br>标记替换\ n

来自分类Dev

如何用\ n替换</br>和<br/>?

来自分类Dev

\ n对PHP_EOL对<br>?

来自分类Dev

js将\ n替换为<br>并显示

来自分类Dev

具有BeautifulSoup输出<br>而不是<br/>

来自分类Dev

php替换多个br标签

来自分类Dev

“ <br /> \ n”;和“ <br />”之间有什么区别?

来自分类Dev

在fpdf中将<br />替换为“ \ n”

来自分类Dev

快速将<br>替换为\ n

来自分类Dev

将\ r \ n替换为<br />作为文本

来自分类Dev

在C#中将\ n替换为<br/>

来自分类Dev

放开多个/重复的br标签

来自分类Dev

php mail()/ r / n和<br>无效

来自分类Dev

如何用\ n替换</br>和<br/>?

来自分类Dev

将* <BR>替换为<li>

来自分类Dev

js将\ n替换为<br>并显示

来自分类Dev

如何使用jQuery用\ r \ n替换所有<br />

来自分类Dev

如何在PHP中将两个以上的<br/>标记转换为双精度<br/> <br/>

来自分类Dev

用javascript替换多个<br>标签

来自分类Dev

php替换多个br标签

来自分类Dev

<br> <br> <br>仅在Inspect元素中生成并显示

来自分类Dev

将\ n或\ r更改为<br/>

来自分类Dev

如何同时替换<br> </br>和删除href属性?

来自分类Dev

用<br>替换

来自分类Dev

jQuery用<br>替换\ n HTML无法呈现<br>

来自分类Dev

spaCy 和文本清理,摆脱 '<br /><br />'