I parse the string before sending it to the database. I want to pass everything <br>on this line and replace them with the unique numbers that I get from the array, followed by a new line.
For instance:
str = "Line <br> Line <br> Line <br> Line <br>"
$replace = array("1", "2", "3", "4");
my function would return
"Line 1 \n Line 2 \n Line 3 \n Line 4 \n"
That sounds easy enough. I just execute a while loop, get all the events <br>using strpos and replace them with the required numbers + \ n using str_replace.
The problem is that I always get an error and I have no idea what I'm doing wrong? Probably a stupid mistake, but still annoying.
Here is my code
$str = "Line <br> Line <br> Line <br> Line <br>";
$replace = array("1", "2", "3", "4");
$replaceIndex = 0;
while(strpos($str, '<br>') != false )
{
$str = str_replace('<br>', $replace[index] . ' ' .'\n', $str);
index++;
}
Any ideas please?
Thanks in advance,
source
share