PHP - 如何在换行前删除带有'>'的换行符

问题描述 投票:-3回答:1

我有这样的输入文字

bla;bla<ul>
<li>line one</li>
<li>line one</li>
<ul>bla
line two
line tree

我只想用空格替换包含'>'的行;在行尾没有'>'的其他行将被忽略。

输出应该是:

bla;bla<ul><li>line one</li><li>line one</li><ul>bla
line two
line tree

要替换该行的PHP代码应该是什么?

我试过了

$output = preg_replace( "/\r|\n/", "", $text );

但这不是一个好主意,因为该代码将应用于$ text的所有行

非常感谢。

现在我可以解决这个问题了

$output = preg_replace("/(?<=>)\s+(?=)/", "", $text );

非常感谢

php regex preg-replace
1个回答
0
投票

您可以使用>(?:\n|\r\n)正则表达式并将其替换为>,它将匹配仅位于行尾的>

$text = "bla;bla<ul>\n<li>line one</li>\n<li>line one</li>\n<ul>bla\nline two\nline tree";
$output = preg_replace( "/>(?:\n|\r\n)/", ">", $text );
echo $output;

这给出了你期望的以下输出,

bla;bla<ul><li>line one</li><li>line one</li><ul>bla
line two
line tree

Live Demo

© www.soinside.com 2019 - 2024. All rights reserved.