I have a piece of string like:
Hello <br>everybody. <br>This <br>is <br>my <br>question.
How could I remove the 4th <br>
with a method that searches this string from the beginning?
//expected result
Hello <br>everybody. <br>This <br>is my <br>question.
you could do it with explode and implode to remove the 4th break:
$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);
Do it with CSS:
br:nth-child(4)
{
display:none;
}
Credits for function to: https://stackoverflow.com/a/18589825/1800854
Find the position of
and use that to make substring without the fourth
.
<?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;
?>