Untitled
By: a guest | Oct 6th, 2008 | Syntax:
None | Size: 1.74 KB | Hits: 515 | Expires: Never
<?php
function parse_bb($text)
{
while (preg_match('#(?<!UNKNOWN:)\[(.+)(?:=(.*?))?\](.*?)\[\/\1\]#s',$text))
{
$text = preg_replace_callback('#(?<!UNKNOWN:)\[(.+)(?:=(.*?))?\](.*?)\[\/\1\]#s', 'parse_bb_callback', $text);
}
return nl2br($text);
}
function parse_bb_callback($hit)
{
$tag = $hit[1];
$attr = $hit[2];
$encl = $hit[3];
switch ( $tag )
{
case 'b': case 'li': case 'ol': case 'i': case 'u': case 'h3':
return "<$tag>$encl</$tag>";
break;
case 'img':
return "<img src=\"$attr\" alt=\"$encl\" />";
break;
case 'lnk':
if ( $encl == '' )
{
return "<a href=\"$attr\" target=\"_blank\">$attr</a>";
}
return "<a href=\"$attr\">$encl</a>";
break;
case 'mailto':
$mail = preg_replace( "/(.)/se", " '&#' . ord( '\\1' ) . ';' ", $attr );
return "<a href=\"mailto:$mail\">$mail</a>";
break;
case 'html':
$encl = html_entity_decode($encl);
$encl = str_replace('&', '&', $encl);
return $encl;
break;
default:
return "UNKNOWN:$hit[0]";
}
}
$html='[ol]
[li]list item[/li]
[li]another list item[/li]
[/ol]
[i][b]test[/b]ing[/i]
[img=path]image[/img]
[othertag=attr]encl[/othertag]';
echo parse_bb($html);
?>