Witam. Przysiadłem nad funkcją do zamiany BBCode na HTML, którą kiedyś na szybko napisałem, a która nie była pozbawiona wad. Oto wersja 2.0

Proszę testować i zgłaszać błędy.
<?php
function bb2html( $str )
{
$codesArr = array( 'url' => array( 'a', 'href=\"' ), 'img' => array( 'img', 'src=\"' ), 'color' => array( 'span', 'style=\"color:' ), 'font' => array( 'span', 'style=\"font-family:' ), 'table' => array( 'table', 'table' ), 'b' => 'b',
'u' => 'u',
'i' => 'i',
'ul' => 'ul',
'li' => 'li' );
foreach( $codesArr as $aKey => $aVal )
{
$str = preg_replace_callback( \"#[($aKey)+[=]?(http|https|ftp+://)?(.*?)](http|https|ftp+://)?(.*?)[/$aKey]#si\", 'repl', $str ); }
return( $str );
}
function repl( $matches )
{
if( is_array( $codesArr[$matches[1
]] ) ) {
if( !empty( $matches[2
] ) ) {
$protocol = $matches[2];
}
elseif( !empty( $matches[4
] ) ) {
$protocol = $matches[4];
}
else
{
$protocol = 'http://';
}
switch( $matches[1] )
{
case 'url':
$address = empty( $matches[3
] ) ?
$matches[5
] : $matches[3
]; $x = '<' . implode( ' ', $codesArr[$matches[1
]] ) . $protocol . $address . '\" class=\"link1\" target=\"_blank\">'; $x .= $matches[4] . $matches[5];
$x .= '</' . $codesArr[$matches[1]][0] . '>';
break;
case 'img':
$address = empty( $matches[3
] ) ?
$matches[4
] . $matches[5
] : $matches[2
] . $matches[3
]; $x = '<' . implode( ' ', $codesArr[$matches[1
]] ) . $address . '\" class=\"icon\" alt=\"\" />'; break;
case 'table':
$temp = explode( \"n\", str_replace( \"r\", '', $matches[5] ) ); $rows = '';
$colsNo = 1;
foreach( $temp as $row )
{
{
$rows .= '<tr><td>' . str_replace( '|', '</td><td>', $row ) . '</td></tr>'; }
}
if( !empty( $matches[3
] ) ) {
$headInfo = explode( '|', $matches[3
] ); $head = ( !empty( $headInfo[0] ) ) ?
'<tr><th colspan=\"' . $colsNo . '\">' . $headInfo[0] . '</th></tr>' : ''; $border = ( is_numeric( $headInfo[1
] ) ) ?
$headInfo[1
] : 1; $width = ( !empty( $headInfo[2
] ) ) ?
$headInfo[2] : ''; }
$x = !empty( $rows ) ?
'<'.$codesArr[$matches[1]][0].' border=\"'.$border.'\" width=\"'.$width.'\">'.$head.$rows.'</'.$codesArr[$matches[1]][1].'>' : ''; break;
default:
$x = '<' . implode( ' ', $codesArr[$matches[1
]] ) . $matches[3] . '\">'; $x .= $matches[4] . $matches[5];
$x .= '</' . $codesArr[$matches[1]][0] . '>';
break;
}
}
else
{
$x = '<' . $codesArr[$matches[1]] . '>' . $matches[5] . '</' . $codesArr[$matches[1]] . '>';
}
return $x;
}
?>
Możliwe BBCode'y:
Kod
[url]www.adres.pl[/url]
[url]http://www.adres.pl[/url]
[url=www.adres.pl]text[/url]
[url=http://www.adres.pl]text[/url]
[url=https://www.adres.pl]text[/url]
[url]https://www.adres.pl[/url]
[url=ftp://ftp.adres.pl]text[/url]
[url]ftp://ftp.adres.pl[/url]
[img]http://www.adres.pl/image.xxx[/img]
[img=http://www.adres.pl/image.xxx][/img]
[color=kolor]text[/color]
[b]text[/b]
[i]text[/i]
[u]text[/u]
[font=kroj_czcionki]text[/font]
[table] element 1 | element2 | element3
element 4 | element5 | element6[/table]
[ul][li]element1[/li] [li]element2[/li][/ul]
Oczywiście można też samemu dorobić odpowiednie BBCode'y

Definicja funkcji:
string <span style="font-weight: bold;">bb2html</span>( string )
Ten post edytował BzikOS 24.08.2006, 12:22:47