Advanced str_replace context problem
Posted: Thu Aug 27, 2009 1:15 am
I'm exceptionally close to getting my BB code to XHTML validator/parser to working without a hitch however there is an advanced issue with PHP's str_replace function that I simply can't figure out how to deal with. I've setup a testing area on my site that once I'm absolutely sure it's rock solid I'll post over in the critique forum however I need some help with this issue first please.
The function below is fine when I'm dealing with simple BB tags, however when I need to find and replace three strings everything goes hay-wire. There are two problems with this. First I'm not sure how to only replace the ending bracket of the first BB tag (]). This one small bracket is causing all the chaos for me.
I'm not sure if there is a way even after reading php.net to force converting only the bracket if it's within say a certain range of the other string parameters. Since it applies to all brackets this opens a hole up to non-BB code bracket such as [i-do-not-exist] converting the ending bracket to the second half of the opening anchor element.
The only remote idea I've had is to somehow go through and replace all the end-brackets in the initial BB tags and then later run another str_replace for those strings later on...but then I'm not sure how to do that as the string *before* the bracket and *after* the bracket is still subjective content!
Thoughts please?
Since this is dealing with BB code I have to disable the BBcode parsing.
________________________
function bb_3_xhtml($text0)
{
$bb1 = array('<', '>');
$xml1 = array('<', '>');
$bb2 = array(
'[b]','[/b]',
'[syntax=php]','[/syntax]',
//'[color=','[/color]',']',
'[i]','[/i]',
'[q]','[/q]',
'[u]','[/u]',
'[quote]', '[/quote]',
//'[list]', '[*]', '[/list]',
);
$xml2 = array(
'<b>','</b>',//'<span class="b">','</span>',
'<code><pre>','</pre></code>',
//'<span style="color: ', '</span>',';">',
'<i>','</i>',//'<span class="i">','</span>',
'<q>','</q>',
'<u>','</u>',//'<span class="u">','</span>',
//'<ul>', '<li>', '</ul>',
"\n<blockquote>", "<blockquote>\n",
);
$bb3 = array('[img]','[/img]');
$xml3 = array('<img alt="" src="', '" />');
$bb4 = array('[size="','[/size]','"]');
$xml4 = array('<span style="font-size: ', '</span>','px;">');
$bb5 = array('[url=','[/url]',']');
$xml5 = array('<a class="icon external" href="', '</a>','" rel="nofollow" tabindex="3">');
$text1 = str_replace($bb1, $xml1, $text0);
$text2 = str_replace($bb2, $xml2, $text1);
$text3 = str_replace($bb3, $xml3, $text2);
$text4 = str_replace($bb4, $xml4, $text3);
$text5 = str_replace($bb5, $xml5, $text4);
$result = bb_4_n2p($text5);
return $result;
}
The function below is fine when I'm dealing with simple BB tags, however when I need to find and replace three strings everything goes hay-wire. There are two problems with this. First I'm not sure how to only replace the ending bracket of the first BB tag (]). This one small bracket is causing all the chaos for me.
The only remote idea I've had is to somehow go through and replace all the end-brackets in the initial BB tags and then later run another str_replace for those strings later on...but then I'm not sure how to do that as the string *before* the bracket and *after* the bracket is still subjective content!
Thoughts please?
Since this is dealing with BB code I have to disable the BBcode parsing.
________________________
function bb_3_xhtml($text0)
{
$bb1 = array('<', '>');
$xml1 = array('<', '>');
$bb2 = array(
'[b]','[/b]',
'[syntax=php]','[/syntax]',
//'[color=','[/color]',']',
'[i]','[/i]',
'[q]','[/q]',
'[u]','[/u]',
'[quote]', '[/quote]',
//'[list]', '[*]', '[/list]',
);
$xml2 = array(
'<b>','</b>',//'<span class="b">','</span>',
'<code><pre>','</pre></code>',
//'<span style="color: ', '</span>',';">',
'<i>','</i>',//'<span class="i">','</span>',
'<q>','</q>',
'<u>','</u>',//'<span class="u">','</span>',
//'<ul>', '<li>', '</ul>',
"\n<blockquote>", "<blockquote>\n",
);
$bb3 = array('[img]','[/img]');
$xml3 = array('<img alt="" src="', '" />');
$bb4 = array('[size="','[/size]','"]');
$xml4 = array('<span style="font-size: ', '</span>','px;">');
$bb5 = array('[url=','[/url]',']');
$xml5 = array('<a class="icon external" href="', '</a>','" rel="nofollow" tabindex="3">');
$text1 = str_replace($bb1, $xml1, $text0);
$text2 = str_replace($bb2, $xml2, $text1);
$text3 = str_replace($bb3, $xml3, $text2);
$text4 = str_replace($bb4, $xml4, $text3);
$text5 = str_replace($bb5, $xml5, $text4);
$result = bb_4_n2p($text5);
return $result;
}