Don't use this function to fetch BBCode, as explained. If you have some text that runs over 5000 chars (average), it will run out of its limit and makes you download the PHP page.
According to this, you should instead use something more advanced yet complex. You will need a function called "str_replace_once()" (search for it), one called "countWord()", the famous "after()", "before()", "between()".
str_replace_once does same as str_replace, but only replace first occurence. As for countWord, I guess you know how to count the number of a word occurence. As for after, before and between, this is a function that you may find easily somewhere on the site by a user. Else, you can do it.
The following function is able to do all blocks, supposing [code] and [/code], you might wish things between parents dont get parsed, including [code] if inside of another [code].
<?php
function prepareCode($code, $op, $end)
{
$ix = 0;
$iy = 0;
$nbr_Op = countWord($op, $code);
while($ix < $nbr_Op)
{
if(in_string($op, before($end, $code), false))
{
$code = str_replace_once($op, substr($op, 0, -1).':'.$ix.']', $code);
$iy++;
}
elseif(in_string($end, before($op, $code), false))
{
$iy = $iy-1;
$code = str_replace_once($end, substr($end, 0, -1).':'.($ix-1).']', $code);
$ix = $ix-2;
}
$ix++;
}
while(in_string($end, $code))
{
$code = str_replace_once($end, substr($end, 0, -1).':'.($iy-1).']', $code);
$iy=$iy-1;
}
$code = preg_replace('#\\'.substr($end, 0, 1).':-[0-9]\]#i', '', $code);
if(in_string(substr($op, 0, -1).':0]', $code) && !in_string(substr($end, 0, -1).':0]', $code))
{
$code .= substr($end, 0, -1).":0]";
}
return $code;
}
?>
$code returns the whole text semi-formated. You only need to use it as :
$code = prepareCode($code="Your text", $op="[tag]" , $end="[/tag]");
Then just replace the parent tags :
str_replace("[tag:0]", "<tag>", $code);
str_replace("[/tag:0]", "</tag>", $code);
So at the end something like :
[