Since my code contains BB code I'm just posting the code at the bottom of my post.
So this pretty much does everything so far with only a couple notes.
First I'm not wild about implementing font size with quotes, I'll likely look at existing implementations and already have looked at how phpBB implements it. The converter function does have it's limitations even after implementing it to execute multiple passes so I'm looking to make the more commonly typed BB codes work as presumed and BB codes that are rarely typed to be subjective to the replacement function and thus how it's implemented.
Also how it handles BB to XHTML code element needs a lot of attention. I've thrown in a pre element though currently if there are break lines between code elements it'll either fail or do something funky. I'm going to look in to existing PHP functions to support advanced conversion of code such as how phpBB forums here work with code.
However beyond the font size and code elements I haven't been able to find any bugs. It could definitely use more polish and perhaps better error handling though all good things with time.
Here is my latest build...
_______________________________
<?php
//$multiple_line = "stuff[b]stuff![/b]\n[i][b]stuff[/b][/i][quote][quote][quote][quote][url=http:///www.jabcreations.com/]stuff[/url][/quote][/quote][/quote][/quote][url=
http://www.example.com/]example[/url][b]stuff![/b]\nstuff[b]stuff![/b]\n";
$multiple_line = '<a href="stuff">stuff!</a>[quote][size="24"][color="red"]big red text![/color][/size] [color="orange"]orange text![/color] link = [url=
http://forums.devnetwork.net/]DevNetwork[/url][img]
styles/subsilver2/imageset/site_logo.gif[/img][b]bold text![/b][i]italic![/i][/quote]'."\n".'stuff!';
//$multiple_line = '[url=
http://www.jabcreations.com]JAB Creations[/url][img]
styles/subsilver2/imageset/site_logo.gif[/img]';
//$multiple_line = '[url=
http://www.jabcreations.com][img]
styles/subsilver2/imageset/site_logo.gif[/img][/url]';
echo bb_1($multiple_line);
function bb_1($pizza1)
{
$pieces1 = explode("\n", $pizza1);
foreach($pieces1 as $key => $value)
{
$result = bb_2_validator($value);
if ($result!='valid') {return $result; break;}
}
if ($result=='valid') {$result = bb_3_xhtml($pizza1);}
echo $result;
}
function bb_2_validator($pizza1)
{
$pieces1 = explode("[", $pizza1);
//echo '<div><pre>';
//print_r($pieces1);
//echo '</pre></div>';
$c = count($pieces1);
$i = '1';
$bb_open = array();
$bb_allowed = array('b','code','color','i','img','q','quote','size','u','url');
foreach($pieces1 as $key => $value)
{
//echo '<div>i = '.$i.' and c = '.$c.'</div>';
$pieces2 = explode("]", $value);
//echo '<div>pieces1 == '.$pieces2[0].'</div>';
// Allow blockquote nesting though prevent inline elements from having blockquotes nested within them...
if ($pieces2[0]=='quote')
{
foreach($bb_open as $key1 => $value1)
{
//echo '<div>val1 = '.$value1.'</div>';
if ($value1!='quote') {$result = 'quote block element nested inside of inline element!'; break;}
}
}
$pieces3 = explode("=", $pieces2[0]);
// img and url validation!!!!!!!!!!!
if (count($pieces3)=='2')
{
//echo '<div>'.$pieces3[0].'</div>';
if (substr($pieces3[1], 0,7)!='http://')
{
if ($pieces3[0]=='img') {$result = 'invalid img url'; break;}
else if ($pieces3[0]=='url') {$result = 'invalid url'; break;}
}
}
if (!empty($pieces3[0]) && count($pieces2)=='2')
{
if ($pieces3[0][0]!='/')
{
// Opening BB Code
// Add BB code to end of array to be compared with next closing BB code.
if ($i!=$c)
{
if (in_array($pieces3[0],$bb_allowed))
{
array_push($bb_open,$pieces3[0]);
//echo '<div>';
//print_r($bb_open);
//echo '</div>';
}
else {$result = '<div>INVALID! element not in white list! == '.$pieces3[0]; break;}
}
else {$result = '<div>INVALID! Last BB is opening when should close!</div>'; break;}
}
else if ($pieces3[0][0]=='/')
{
$pieces4 = explode("/", $pieces3[0]);
//echo '<div>pieces2 == '.$pieces4[1].'</div>';
// Closing BB Code
// Compare to last element in $bb_code opened tag, if not a match then code is invalid!
//if ($pieces4[1]==end($bb_open)) {$result = '<div>'.$pieces4[1].' == '.end($bb_open).' == VALID!</div>';}
//else {$result = '<div>'.$pieces4[1].' == '.end($bb_open).' == INVALID!</div>';}
if ($pieces4[1]!=end($bb_open)) {$result = '<div>'.$pieces4[1].' == '.end($bb_open).' == INVALID!</div>'; break;}
array_pop($bb_open);
}
}
//echo '<br />';
//echo '<div>'.$i.'</div>';
$i++;
}
if (!isset($result)) {$result = 'valid';}
return $result;
}
function bb_3_xhtml($text0)
{
$bb1 = array('<', '>');
$xml1 = array('<', '>');
$bb2 = array(
'[b]','[/b]',
'[syntax=php]<div class=\"text\" id=\"{CB}\" style=\"font-family: monospace;\"><ol><li style=\"\" class=\"li1\">','</li></ol></div>[/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;
}
function bb_4_n2p($pizza)
{
$pieces = explode("\n",$pizza);
foreach($pieces as $key => $value) {if ($value=="") {unset($pieces[$key]);}}
foreach($pieces as $key => $value)
{
$bb_q = explode("<blockquote>",$value);
$bb_c = explode("<code>",$value);
if (count($bb_c)=='1' && count($bb_q)=='1') {$result .= '<p>'.$value."</p>\n\n";}
else {$result .= $value."\n\n";}
}
return $result;
}
?>