PHP - 如何在换行前删除带有“>”的换行符
PHP - How to remove newline with '>' before line break
我有这样的输入文本
bla;bla<ul>
<li>line one</li>
<li>line one</li>
<ul>bla
line two
line tree
我只想将包含“>”的行替换为空白space;
行尾没有'>'的其他行将被忽略。
输出应该是:
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 );
非常感谢
您可以使用 >(?:\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
我有这样的输入文本
bla;bla<ul>
<li>line one</li>
<li>line one</li>
<ul>bla
line two
line tree
我只想将包含“>”的行替换为空白space; 行尾没有'>'的其他行将被忽略。
输出应该是:
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 );
非常感谢
您可以使用 >(?:\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