-3

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. 
Lewis
  • 14,132
  • 12
  • 66
  • 87
  • 5
    This question appears to be off-topic because it simply asks for code without attempting to solve the problem. – Anonymous Apr 03 '14 at 11:06
  • Refer to: http://stackoverflow.com/questions/3126324/question-about-strpos-how-to-get-2nd-occurrence-of-the-string A recursive implementation using strpos will allow you to find the position of the string. Then simply remove it. – Mo Beigi Apr 03 '14 at 11:06

3 Answers3

3

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);
Steve
  • 20,703
  • 5
  • 41
  • 67
1

Do it with CSS:

br:nth-child(4)
{
    display:none;
}
effone
  • 2,053
  • 1
  • 11
  • 16
0

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;

?>
Community
  • 1
  • 1
Mo Beigi
  • 1,614
  • 4
  • 29
  • 50