Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中删除文本中的一个额外换行符_Php_Regex_Preg Replace - Fatal编程技术网

在php中删除文本中的一个额外换行符

在php中删除文本中的一个额外换行符,php,regex,preg-replace,Php,Regex,Preg Replace,我的问题和你的正好相反。 需要删除最后一个br标记 输入: Test1 is here<br><br>Now comes Test2<br><br>Then test 3<br><br><br>Thats it. Test1在这里现在是Test2然后是test3就是这样。 输出 Test1 is here<br>Now comes Test2<br>Then test 3<br

我的问题和你的正好相反。 需要删除最后一个br标记

输入:

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

现在是Test2

然后是test3


就是这样。
输出

 Test1 is here<br>Now comes Test2<br>Then test 3<br><br>Thats it.
Test1在这里
现在是Test2
然后是test3

就是这样。
我的尝试:

preg_replace("[((?:<br>)+)]","",$posttext)
preg_replace(“[((?:
)+)]”,“”,$posttext)
它将删除所有中断。

您可以替换

<br><br>(?!<br)



(?!尽情欣赏这首歌,哈哈哈

如果Preg replace不起作用

// cuts off one <br> as high as whatever $i is at

$string = "Test1 is here<br><br>Now comes Test2<br><br>Then test 3<br><br><br>Thats it.";
$i = 10;

while($i > 0)
{
  $ii = 1;
  $brake = "<br>";
  $replace = "";
  while($ii < $i)
  {
   $brake .= "<br>";
   $replace .= "<br>";
   $ii++;
  }
$string = str_replace($brake,$replace,$string);
$i--; 
} 

 echo $string; // Test1 is here<br>Now comes Test2<br>Then test 3<br><br>Thats it.
//切掉一个与$i的值一样高的值
$string=“Test1在这里

现在是Test2

然后是test3

就这样了。”; $i=10; 而($i>0) { 二元=1; $brake=“
”; $replace=“”; 而($ii<$i) { $brake.=“
”; $replace.=“
”; $ii++; } $string=str_replace($brake,$replace,$string); $i--; } echo$string;//这里是Test1
现在是Test2
然后是test3

就是这样。

PS:如果没有preg replace,它是可用的,尽管效率很低。

输出是Test1在这里
现在是Test2
然后是test3
就是这样。或者Test1在这里
现在是Test2
然后是test3
就是这样。@Faresoft o/p需要的是Test1在这里
现在是Test2
然后是test3
就是这样。删除的答案会有almost工作正常-只需将
+
移到括号中:
/(
+)
/
,如果中间有空格,请使用
/
\s*
(?!\s*
// cuts off one <br> as high as whatever $i is at

$string = "Test1 is here<br><br>Now comes Test2<br><br>Then test 3<br><br><br>Thats it.";
$i = 10;

while($i > 0)
{
  $ii = 1;
  $brake = "<br>";
  $replace = "";
  while($ii < $i)
  {
   $brake .= "<br>";
   $replace .= "<br>";
   $ii++;
  }
$string = str_replace($brake,$replace,$string);
$i--; 
} 

 echo $string; // Test1 is here<br>Now comes Test2<br>Then test 3<br><br>Thats it.