我有一个字符串:
Hello <br>everybody. <br>This <br>is <br>my <br>question.
如何使用从头开始搜索此字符串的方法删除第4个<br>
?
//expected result
Hello <br>everybody. <br>This <br>is my <br>question.
答案 0 :(得分:3)
你可以通过爆炸和内爆来消除第4次突破:
$st='Hello <br>everybody. <br>This <br>is <br>my <br>question.';
$ar=explode('<br>', $st);
$ar[3].=$ar[4];
unset($ar[4]);
echo implode('<br>', $ar);
答案 1 :(得分:1)
使用CSS:
br:nth-child(4)
{
display:none;
}
答案 2 :(得分:0)
功能的归功于:https://stackoverflow.com/a/18589825/1800854
找到
的位置并使用它来制作没有第四个的子串。
<?php
function strposX($haystack, $needle, $number){
if($number == '1'){
return strpos($haystack, $needle);
}elseif($number > '1'){
return strpos($haystack, $needle, strposX($haystack, $needle, $number - 1) + strlen($needle));
}else{
return error_log('Error: Value for parameter $number is out of range');
}
}
$str = "Hello <br>everybody. <br>This <br>is <br>my <br>question.";
$remove = "<br>";
$pos = strposX($str, $remove, 4);
if ($pos)
$str = substr($str, 0, $pos) . substr($str, $pos+ strlen($remove), strlen($str));
echo $str;
?>