Grubber wrote:I'm thinking about rewriting some parts of the forum template to be more XHTML-friendly - it pisses me that the main page is valid XHTML, but only without the stuff fetched from forum.
I've been using this function I wrote to convert most of the BBCode to XHTML. Note that you have to set a fetch variable so it works properly.
Code: Select all
$CFG['posts_enable_bbcode'] = false;
Code: Select all
///////////////////////////////////////////////////////
// XHTM_BBCode : Converts most BBCode to valid XHTML or just butchers the code mercilessly
function XHTM_BBCode($in){
while(preg_match("/\[b\](.+?)\[\/b\]/i",$in,$out)){
$in=str_replace($out[0],"<b>".$out[1]."</b>",$in);
}while(preg_match("/\[i\](.+?)\[\/i\]/i",$in,$out)){
$in=str_replace($out[0],"<i>".$out[1]."</i>",$in);
}while(preg_match("/\[url=(.+?)\](.+?)\[\/url\]/i",$in,$out)){
$in=str_replace($out[0],"<a href="".$out[1]."">".$out[2]."</a>",$in);
}while(preg_match("/\[size=(.+?)\](.+?)\[\/size\]/i",$in,$out)){
$in=str_replace($out[0],"<span style="font-size:".($out[1]*2)."px">".$out[2]."</span>",$in);
}while(preg_match("/\[img\](.+?)\[\/img\]/i",$in,$out)){
$in=str_replace($out[0],"<img src="".$out[1]."" alt=""/>",$in);
}
// IYAA! I hate to do all these with <span>s
while(preg_match("/\[quote:.+?="(.+?)"\](.+?)\[\/quote\]/i",$in,$out)){
$in=str_replace($out[0],"<br/><span class="quote">".$out[1]." wrote:<span class="quotetxt">".$out[2]."</span></span>",$in);
}while(preg_match("/\[quote\](.+?)\[\/quote\]/i",$in,$out)){
$in=str_replace($out[0],"<br/><span class="quote">Quote:<span class="quotetxt">".$out[1]."</span></span>",$in);
}while(preg_match("/\[code\](.+?)\[\/code\]/i",$in,$out)){
$in=str_replace($out[0],"<br/><span class="quote">Code:<span class="code">".str_replace(" "," ",$out[1])."</span></span>",$in);
}
// GAH! Lists!
while(preg_match("/\[\*\](.+?)\[\*\]/i",$in,$out)){
$in=str_replace($out[0],"<li>".$out[1]."</li>[*]",$in);
}while(preg_match("/\[\*\](.+?)\[\/list\]/i",$in,$out)){
$in=str_replace($out[0],"<li>".$out[1]."</li>[/list]",$in);
}while(preg_match("/\[list\](.+?)\[\/list\]/i",$in,$out)){
$in=str_replace($out[0],"<ul>".str_replace("<br/>","",$out[1])."</ul>",$in);
}
$in = str_replace("<ul></li>","<ul>",$in); // nested lists quirk #1
$in = str_replace("</ul>","</li></ul>",$in); // nested lists quirk #2
$in = str_replace("</li></li></ul>","</li></ul>",$in); // nested lists quirk #3
while(preg_match("/<br\/><ul>(.+?)<\/ul>\s/i",$in,$out)){ // DIRTIEST HACK EVER
$in=str_replace($out[0],"</p><ul>".$out[1]."</ul><p>\n",$in);
}
return $in;
}