I am parsing a string before sending it to a DB. I want to go over all <br>
in that string and replace them with unique numbers that I get from an array followed by a newLine.
For example:
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"
Sounds simple enough. I would just do a while loop, get all the occurances of <br>
using strpos, and replace those with the required numbers+\n using str_replace.
Problem is that I always get an error and I have no idea what I am doing wrong? Probably a dumb 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); //str_replace, replaces the first occurance of <br> it finds
index++;
}
Any ideas please?
Thanks in advance,
`.... To do it this way you would have to only replce parts of the string using `substr_replace` which lets you define the postions for he replacement. – prodigitalson Apr 22 '13 at 16:44