在php中的文本中添加一个额外的换行符

在php中的文本中添加一个额外的换行符,php,Php,我想把Text1改成Text2 Text1 Test1在这里现在是Test2然后是test3就是这样。 Text2 Test1在这里现在是Test2然后是test3就是这样。 i、 e;将额外的“特征线”标记添加到字符串中的现有标记 我用preg_replace试过了,但没能找到我想要的方法 我的尝试- preg\u replace('/(?:(?:)\s*)/s',“”,$posttext) 这应该可以做到: $text = preg_replace('/((<br>(\s

我想把Text1改成Text2

Text1

Test1在这里
现在是Test2
然后是test3

就是这样。
Text2

Test1在这里

现在是Test2

然后是test3


就是这样。
i、 e;将额外的“特征线”标记添加到字符串中的现有标记

我用preg_replace试过了,但没能找到我想要的方法

我的尝试-

preg\u replace('/(?:(?:
)\s*)/s',“

”,$posttext)
这应该可以做到:

$text = preg_replace('/((<br>(\s+)?)+)/', '$1<br>', $text);
$text=preg_replace(“/”(
(\s+)+)/”,“$1
”,$text);
如果不想允许使用换行符和空格,请尝试:
/(
)+)/

尝试以下操作:

preg_replace('/((?:<br>)+)\s*/s', "$1<br>", $posttext);
preg_replace('/((?:
)+)\s*/s',“$1
”,$posttext);
这将捕获一系列的

标记,可选地后跟空格,然后在它们之后再添加一个

试试这个

$text1 = "Test1 is here<br>Now comes Test2<br>Then test 3<br><br>Thats it.";
$text2 = substr($text1,0,strripos($text1,"<br>")) ."<br>" . substr($text1,strripos($text1,"<br>"));
$text1=“Test1在这里
现在是Test2
然后是test3

就这样了。”; $text2=substr($text1,0,stripos($text1,“
”)。“
”。substr($text1,stripos($text1,“
”);
您的php脚本具体在哪里?@davejal刚刚添加到上面,您不需要任何捕获组。它将1到2个中断转换为2到3个中断,但无法修复2到3个中断,并允许换行符和空格。它将2个中断转换为3个中断,但无法将1转换为2个中断。
$text1 = "Test1 is here<br>Now comes Test2<br>Then test 3<br><br>Thats it.";
$text2 = substr($text1,0,strripos($text1,"<br>")) ."<br>" . substr($text1,strripos($text1,"<br>"));