Hier ist eine von mir geschrieben leicht erweiterbare PHP BBCode Klasse. Außerdem maskiert sie unnötige HTML Zeichen und ersetzt \n durch

<br />’s!

Standartmäßig sind enthalten im PHP Script enthalten:

<a>, <b>, <img><i> und auch einen youtube BBCode:

<?php

class BBCodeParser
{
	private $BBCodesEasy = array("b", "url", "img", "i", "youtube");
	private $rawText = "";

	public function __construct()
	{
		if(!is_array($this->BBCodesEasy))
		{
			trigger_error("Bitte trage die zu parsenden BBCodes ein");
		}
	}

	public function iniParse($rawText)
	{
		foreach($this->BBCodesEasy[0] as $key => $BBCode)
		{
			if(stripos($rawText, "[".$BBCode."]") !== FALSE && stripos($rawText, "[/".$BBCode."]") !== FALSE)
			{
				return true;
			}
			continue;
		}
		return false;
	}

	public function parseText()
	{
		if($this->rawText == "")
		{
			trigger_error("Diese Funktion muss erst durch iniParse() initialisert werden!");
		}
		$this->rawText = preg_replace("/\[b\](.*)\[\/b\]/isU", "<b>$1</b>", $this->rawText);
		$this->rawText = preg_replace("/\[i\](.*)\[\/i\]/isU", "<i>$1</i>", $this->rawText);
		$this->rawText = preg_replace("/\[img\](.*)\[\/img\]/isU", "<img src=\"$1\" alt=\"\" title=\"\" />", $this->rawText);
		$this->rawText = preg_replace("/\[url\=(.*)\](.*)\[\/url\]/isU", "<a href=\"$1\">$2</a>", $this->rawText);
		$this->rawText = preg_replace("/\[youtube\](.*)\[\/youtube\]/isU", "<object width=\"560\" height=\"340\"><param name=\"movie\" value=\"http://www.youtube.com/v/$1\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"http://www.youtube.com/v/$1\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"560\" height=\"340\"></embed></object>", $this->rawText);
		return $this->rawText;
	}

	public function Parser($rawText)
	{
		$this->rawText = htmlentities($rawText);
		if(!$this->iniParse($this->rawText)) return $this->rawText;
		$this->parseText();
		$this->rawText = nl2br($this->rawText);
		return $this->rawText;
	}
}

$text = "[URL=http://php-friends.de]PHP-Friends[/URL][I][B]Text[/B][B]Text2[/B][/I]\n\n\n[youtube]bGBOKtaGlyA[/youtube]";
$bbcode = new BBCodeParser();
$text2 = "ABC";
echo $bbcode->Parser($text2);
echo $bbcode->Parser($text);
?>

Es sollte eigentlich selbstklärend sein. Nur auf den Youtube BBCode werde ich näher eingehen:

Um diesen zu verwenden müsst ihr den v= Teil aus dem Link zwischen die Tags kopieren. Bei diesem Link:

wäre es LLjj4xvx6zk. Also

[youtube]LLjj4xvx6zk[/youtube]

Bei Fragen etc. wie immer Kommentar oder eMail!

Download:  PHP BBCode Klasse/Class

*UPDATE 21.11.2009: Peinlicher Fehler im Script entdeckt und verbessert*