Jest to mój pierwszy "parser" BBCode, dużo tu opisywać nie trzeba

Kod
<?php
/*****************************************************************
* Fusion.Board
*
* @author Denis Wrobel
*****************************************************************/
class parser
{
private $text; //Text to parse
private $tags = array( 'bold' => array( 'regexp' => '\[b\](.*?)\[\/b\]', 'replace' => '<b>{VALUE_1}</b>',
'embed' => 0 ),
'uline'=> array( 'regexp' => '\[u\](.*?)\[\/u\]', 'replace' => '<u>{VALUE_1}</u>',
'embed' => 0 ),
'italic'=>array( 'regexp' => '\[i\](.*?)\[\/i\]',
'replace' => '<i>{VALUE_1}</i>',
'embed' => 0 ),
'url' => array( 'regexp' => '\[url\](.*?)\[\/url\]', 'replace' => '<a href="{VALUE_1}">{VALUE_1}</a>',
'embed' => 0 ),
'nurl' => array( 'regexp' => '\[url=(.*?)\](.*?)\[\/url\]', 'replace' => '<a href="{VALUE_1}">{VALUE_2}</a>',
'embed' => 0 ),
'sub' => array( 'regexp' => '\[sub\](.*?)\[\/sub\]', 'replace' => '<sub>{VALUE_1}</sub>',
'embed' => 0 ),
'sup' => array( 'regexp' => '\[sup\](.*?)\[\/sup\]', 'replace' => '<sup>{VALUE_1}</sup>',
'embed' => 0 ),
'center'=>array( 'regexp' => '\[center\](.*?)\[\/center\]',
'replace' => '<center>{VALUE_1}</center>',
'embed' => 0 ),
'image'=> array( 'regexp' => '\[img\](.*?)\[\/img\]', 'replace' => '<img src="{VALUE_1}" alt="IMAGE" title="IMAGE"/>',
'embed' => 0 ),
'strike'=>array( 'regexp' => '\[s\](.*?)\[\/s\]',
'replace' => '<s>{VALUE_1}</s>',
'embed' => 0 ),
'quote'=> array( 'regexp' => '\[quote\](.*)\[\/quote\]', 'replace' => '<div class="quote_body"><div class="quote_head">Cytat</div> <div class="quote_text">{VALUE_1}</div></div>',
'embed' => 1 ),
'uquote'=> array('regexp' => '\[quote=(.*)\](.*)\[\/quote\]', 'replace' => '<div class="quote_body"><div class="quote_head">Cytat ({VALUE_1})</div> <div class="quote_text">{VALUE_2}</div></div>',
'embed' => 1 ),
'code' => array( 'regexp' => '\[code\](.*?)\[\/code\]', 'replace' => '<div class="code_body"><div class="code_head">Kod</div> <div class="code_text">{VALUE_1}</div></div>',
'embed' => 1 ),
'color'=> array( 'regexp' => '\[color=(.*)\](.*?)\[\/color\]', 'replace' => '<span style="color: {VALUE_1}">{VALUE_2}</span>',
'embed' => 1 ),
'size' => array( 'regexp' => '\[size=(10|12|14|18|25)\](.*?)\[\/size\]', 'replace' => '<span style="font-size: {VALUE_1}">{VALUE_2}</span>',
'embed' => 1 ),
);
public function parse( $text )
{
$this->text = $text;
$this->text = $this->prepare( $text );
return $this->text;
}
private function prepare( $text )
{
{
if( count( $this->tags ) ) {
foreach( $this->tags AS $tag )
{
$tag['replace'] = preg_replace( '/\{VALUE_([0-9]{0,100})\}/si', '$$1', $tag['replace'] );
//Embed bbcode?
if( $tag['embed'] == 1 )
{
$replacments = 0;
while( true )
{
$text = preg_replace( '/' . $tag['regexp'] . '/siU', $tag['replace'], $text, 999
, $replacments );
if( $replacments == 0 )
{
break;
}
}
}
else
{
$text = preg_replace( '/' . $tag['regexp'] . '/si', $tag['replace'], $text ); }
}
}
}
return $text;
}
}
?>
CSS.quote_body
{
background-color: #e4e4e4;
border: 1px solid #5475fa;
padding: 1px;
margin-top: 3px;
}
.quote_head
{
background-color: #a0b4e7;
color: #265793;
font-weight: bold;
padding: 5px;
font-size: 13px;
}
.quote_text
{
font-family: arial;
font-size: 11px;
padding: 3px;
}
.code_body
{
background-color: #e7f9e3;
border: 1px solid #1f9a25;
padding: 1px;
margin-top: 3px;
}
.code_head
{
color: #67a217;
font-weight: bold;
padding: 5px;
font-size: 13px;
}
.code_text
{
font-family: arial;
font-size: 11px;
padding: 7px;
}
Przepraszam za post pod postem, ale jak dam edytuj, mam dziwną treść wiadomości
Sposób użycia:
CODE
$parser = new parser;
echo $parser->parse('It works');
Licencja: :| róbta co chceta ale bez zmieniania autora w pliku