在 php 的文本中删除一个额外的换行符
Remove one extra line break in a text in php
我的问题与正好相反。
需要删除最后一个 br 标签。
输入:
Test1 is here<br><br>Now comes Test2<br><br>Then test 3<br><br><br>Thats it.
输出
Test1 is here<br>Now comes Test2<br>Then test 3<br><br>Thats it.
我的尝试:
preg_replace("[((?:<br>)+)]","",$posttext)
它删除了所有中断。
你可以替代
<br><br>(?!<br)
到<br>
preg_replace('/<br><br>(?!<br)/', "<br>", $posttext);
lookahead会阻止匹配<br>
大饱眼福哈哈哈
如果 Preg 替换不起作用...
// 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.
PS:如果没有preg replace,虽然效率很低,但还是可以用的。
我的问题与
输入:
Test1 is here<br><br>Now comes Test2<br><br>Then test 3<br><br><br>Thats it.
输出
Test1 is here<br>Now comes Test2<br>Then test 3<br><br>Thats it.
我的尝试:
preg_replace("[((?:<br>)+)]","",$posttext)
它删除了所有中断。
你可以替代
<br><br>(?!<br)
到<br>
preg_replace('/<br><br>(?!<br)/', "<br>", $posttext);
lookahead会阻止匹配<br>
大饱眼福哈哈哈
如果 Preg 替换不起作用...
// 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.
PS:如果没有preg replace,虽然效率很低,但还是可以用的。