<?php
  error_reporting
(E_ALL);

  class 
BBCode
  
{
    public function 
__construct()
    {
    }

    public function 
BBCode($mixedInfo)
    {
      
$stringCode '';

      
$arrayParameter = array ();

      
$booleanMixedInfoIsArray FALSE;

      if (
is_array ($mixedInfo) === TRUE AND count ($mixedInfo) == 4)
      {
        
$stringText $mixedInfo[3];

        
$stringCode strtolower ($mixedInfo[1]);

        
$arrayParameter $this->ParseParameter($mixedInfo[2]);

        
$booleanMixedInfoIsArray TRUE;
      }
      elseif (
is_string ($mixedInfo) === TRUE)
      {
        
$stringText $mixedInfo;
      }
      else
      {
        return 
'';
      }

      if (
$stringCode != 'noparse')
      {
        
$stringText preg_replace ('°\[(br|hr)]°is''[\1][/\1]'$stringText);

        
$p '°\[(\w+)([^]]*)]((?:[^[]|\[(?!/?\1])|(?R))*)\[/\1]°';
        
$stringText preg_replace_callback ($p, array( &$this'BBCode'), $stringText);
      }

      if (
$booleanMixedInfoIsArray === TRUE)
      {
        switch (
$mixedInfo[1])
        {
/*

          // [color]
          $text = preg_replace("|\[color=([a-z])\](.*?)\[/color\]|Uism", '<span style="color:$1">$2</span>', $text);
#          $text = preg_replace("#\[color=([\w][^ \"\n\r\t<]*?)\](.*?)\[/color\]#is", '<span style="color:$1">$2</span>', $text);

          // [size]
          $text = preg_replace("#\[size=([\w][^ \"\n\r\t<]*?)\](.*?)\[/size]#is", '<span style="font-size:$1px">$2</span>', $text);
*/
          
case 'center':
            
$stringText '<div style="text-align: center;">'.$stringText.'</div>';
            break;

          case 
'quote':
            
$stringText '<br /><span style="font-size:80%;">Zitat:</span><div style="border:1pt solid #000000;padding:5px;">'.$stringText.'</div>';
            break;

          case 
'email':
            
$stringText '<a href="mailto:'.$stringText.'">'.$stringText.'</a>';
            break;

          case 
'img':
            
$stringText '<img src="'.$stringText.'" alt="" />';
            break;

          case 
'b':
            
$stringText '<span style="font-weight:bold;">'.$stringText.'</span>';
            break;

          case 
'i':
            
$stringText '<span style="font-style:italic;">'.$stringText.'</span>';
            break;

          case 
'u':
            
$stringText '<span style="text-decoration:underline;">'.$stringText.'</span>';
            break;

          case 
'p':
          case 
'h1':
          case 
'h2':
          case 
'h3':
          case 
'h4':
          case 
'h5':
            
$stringText '<'.$mixedInfo[1].'>'.$stringText.'</'.$mixedInfo[1].'>';
            break;

          case 
'hr':
          case 
'br':
            
$stringText '<'.$mixedInfo[1].' />';
            break;

          case 
'url':
            if (
count ($arrayParameter) == OR array_key_exists ('__INIT__'$arrayParameter) === FALSE)
            {
              
$stringText '<a href="'.$stringText.'">'.$stringText.'</a>';
            }
            else
            {
              
$stringTitle '';
              if (
array_key_exists ('title'$arrayParameter) === TRUE)
              {
                
$stringTitle ' title="'.$arrayParameter['title'].'"';
              }

              
$stringText '<a href="'.$arrayParameter['__INIT__'].'"'.$stringTitle.'>'.$stringText.'</a>';
              unset (
$stringTitle);
            }
            break;

          default:
            
$stringText '['.$mixedInfo[1].']'.$stringText.'[/'.$mixedInfo[1].']';
        }
      }
      return 
$stringText;
    }


    private function 
ParseParameter ($stringParameter)
    {
      
$arrayParameter = array ();

      if (
is_string ($stringParameter) === TRUE AND empty ($stringParameter) === FALSE)
      {
        if (
preg_match_all('°(^|\w+)\=(\"?)([^\"]*?)\2(?: |$)°',
                           
$stringParameter,
                           
$arrayMatches,
                           
PREG_SET_ORDER) > 0)
        {
          foreach (
$arrayMatches AS $integerMatchCount => $arrayMatch)
          {
            if (empty (
$arrayMatch[1]) === TRUE)
            {
              
$stringKey '__INIT__';
            }
            else
            {
              
$stringKey strtolower ($arrayMatch[1]);
            }
            
$arrayParameter[$stringKey] = $arrayMatch[3];
            
$arrayParameter[$integerMatchCount] = &$arrayParameter[$stringKey];

/*                echo '<p>$arrayParameter</p>';
                echo '<pre>';
                print_r($arrayParameter);
                echo '</pre>';

                echo '<p>$arrayMatches</p>';
                echo '<pre>';
                print_r($arrayMatches);
                echo '</pre>';
                echo '<hr />';*/
          
}
          unset (
$arrayMatch);
        }
      }
      return 
$arrayParameter;
    }
  }
?>