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

时间:2015-11-18 15:59:01

标签: php

我想将Text1更改为Text2。

文本1

 NSLog(@"country Name: %@", [NSTimeZone localTimeZone].name);
 NSLog(@"abbrevationbs %@",[NSTimeZone localTimeZone].abbreviation);

文本2

Test1 is here<br>Now comes Test2<br>Then test 3<br><br>Thats it.

即;添加额外的&#39; breakline&#39;标记为字符串中的现有标记。

我用preg_replace尝试了它,但无法按照我想要的方式来理解它。

我的尝试 -

Test1 is here<br><br>Now comes Test2<br><br>Then test 3<br><br><br>Thats it.

3 个答案:

答案 0 :(得分:1)

这应该这样做:

$text = preg_replace('/((<br>(\s+)?)+)/', '$1<br>', $text);

如果您不想允许换行符和空格,请尝试:/((<br>)+)/

答案 1 :(得分:1)

试试这个:

preg_replace('/((?:<br>)+)\s*/s', "$1<br>", $posttext);

这会捕获一系列<br>标记,可选地后跟空格,然后再添加一个标记。

DEMO

答案 2 :(得分:0)

试试这个。

$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>"));