BBCode Klasse Frage?!

Funjoy

Erfahrenes Mitglied
Hallo,

Ich arbeite gerade an einer BB Code Klasse. Ich wollte da mal paar fragen stellen weill ich dafür keine antwort habe :rolleyes:

1. Welche methode ist besser bzw. schneller beim parsen. wenn ich für jeden BBCode eine extra preg_replace() funktion nutze oder wenn ich alles in ein Array mache und nur eine preg_replace() funktion nutze!?

2. Ich habe schon ein HTML syntaxhighlighter programmiert nur habe ich das Problem das er bei ca. 100 Codezeilen sehr lange beim parsen brauch ist das normal? Ich habe das so gemacht:
PHP:
<?php
	function HTML($array)
	  {
		//> Zeilen Zählen.
		$Line = explode("\n",trim($array[1]));
			
		for($cLine=1;$cLine<=count($Line);$cLine++)
		  {
			$Lines .= "&nbsp;".$cLine."&nbsp;<br>";
		}	

		$string = $array[1];
		$string = preg_replace("!<br />!","",$string);
		$string = preg_replace("!\[!siU","[",$string);
		$string = preg_replace("!\]!siU","]",$string);
		
		$string = preg_replace("!\{!siU","{",$string);
		$string = preg_replace("!\}!siU","}",$string);
		
		$string = preg_replace("!'!Ui","'",$string);
		$string = preg_replace("!\"!Ui","&quot;",$string);
		
		//> Text der in Anführungszeichen Steht Blau ausgeben.
		$string = preg_replace_callback("!'(.*)'!siU",array(&$this,"DelColorQuot2"),$string);
		$string = preg_replace_callback("!&quot;(.*)&quot;!siU",array(&$this,"DelColorQuot1"),$string);
		
		//> Schlüsselwörter farblich hervorheben.
		$string = preg_replace("!&amp;([a-zA-Z0-9#]+);!iU","<em><b>&amp;$1;</b></em>",$string);
		$string = preg_replace("!&lt;!siU","<font color=\"#000099\">&lt;",$string);
		$string = preg_replace("!&gt;!siU","&gt;</font>",$string);
		//> Begin Tags einfärben.
		$string = preg_replace("!&lt;(script| script)(.*)&gt;!siU","<font color=\"#990000\">&lt;$1$2&gt;</font>",$string);
		$string = preg_replace("!&lt;( a|a)(.*)&gt;!siU","<font color=\"#009900\">&lt;$1$2&gt;</font>",$string);
		$string = preg_replace("!&lt;( img|img)(.*)&gt;!siU","<font color=\"#990099\">&lt;$1$2&gt;</font>",$string);
		$string = preg_replace("!&lt;( style|style)(.*)&gt;!siU","<font color=\"#990099\">&lt;$1$2&gt;</font>",$string);
		$string = preg_replace("!&lt;( form|form)(.*)&gt;!siU","<font color=\"#FF9900\">&lt;$1$2&gt;</font>",$string);
		$string = preg_replace("!&lt;( textarea|textarea)(.*)&gt;!siU","<font color=\"#FF9900\">&lt;$1$2&gt;</font>",$string);
		$string = preg_replace("!&lt;( select|select)(.*)&gt;!siU","<font color=\"#FF9900\">&lt;$1$2&gt;</font>",$string);
		$string = preg_replace("!&lt;( option|option)(.*)&gt;!siU","<font color=\"#FF9900\">&lt;$1$2&gt;</font>",$string);
		$string = preg_replace("!&lt;( input|input)(.*)&gt;!siU","<font color=\"#FF9900\">&lt;$1$2&gt;</font>",$string);
		$string = preg_replace("!&lt;( table|table)(.*)&gt;!siU","<font color=\"#009999\">&lt;$1$2&gt;</font>",$string);
		$string = preg_replace("!&lt;( th|th)(.*)&gt;!siU","<font color=\"#009999\">&lt;$1$2&gt;</font>",$string);
		$string = preg_replace("!&lt;( td|td)(.*)&gt;!siU","<font color=\"#009999\">&lt;$1$2&gt;</font>",$string);
		$string = preg_replace("!&lt;( tr|tr)(.*)&gt;!siU","<font color=\"#009999\">&lt;$1$2&gt;</font>",$string);
		$string = preg_replace("!&lt;( tbody|tbody)(.*)&gt;!siU","<font color=\"#009999\">&lt;$1$2&gt;</font>",$string);
		$string = preg_replace("!&lt;( tfoot|tfoot)(.*)&gt;!siU","<font color=\"#009999\">&lt;$1$2&gt;</font>",$string);
		$string = preg_replace("!&lt;(script| script)(.*)&gt;(.*)&lt;\/script&gt;!siU","&lt;$1$2&gt;<font color=\"#0000CC\">$3</font>&lt;/script&gt;",$string);
		
		//> End Tags Einfärben.
		$string = preg_replace("!&lt;\/script&gt;!siU","<font color=\"#990000\">&lt;/script&gt;</font>",$string);
		$string = preg_replace("!&lt;\/a&gt;!siU","<font color=\"#009900\">&lt;/a&gt;</font>",$string);
		$string = preg_replace("!&lt;\/style&gt;!siU","<font color=\"#990099\">&lt;/style&gt;</font>",$string);
		$string = preg_replace("!&lt;\/form&gt;!siU","<font color=\"#FF9900\">&lt;/form&gt;</font>",$string);
		$string = preg_replace("!&lt;\/textarea&gt;!siU","<font color=\"#FF9900\">&lt;/textarea&gt;</font>",$string);
		$string = preg_replace("!&lt;\/select&gt;!siU","<font color=\"#FF9900\">&lt;/select&gt;</font>",$string);
		$string = preg_replace("!&lt;\/option&gt;!siU","<font color=\"#FF9900\">&lt;/option&gt;</font>",$string);
		$string = preg_replace("!&lt;\/table&gt;!siU","<font color=\"#009999\">&lt;/table&gt;</font>",$string);
		$string = preg_replace("!&lt;\/th&gt;!siU","<font color=\"#009999\">&lt;/th&gt;</font>",$string);
		$string = preg_replace("!&lt;\/td&gt;!siU","<font color=\"#009999\">&lt;/td&gt;</font>",$string);
		$string = preg_replace("!&lt;\/tr&gt;!siU","<font color=\"#009999\">&lt;/tr&gt;</font>",$string);
		$string = preg_replace("!&lt;\/tbody&gt;!siU","<font color=\"#009999\">&lt;/tbody&gt;</font>",$string);
		$string = preg_replace("!&lt;\/tfoot&gt;!siU","<font color=\"#009999\">&lt;/tfoot&gt;</font>",$string);
		
		//> Kommentare Grau ausgeben.
		$string = preg_replace_callback("!&lt;\!--(.*)--&gt;!siU",array(&$this,"DelColor2"),$string);
		
		//> CSS Farblich hervorheben.
		$string = preg_replace_callback("!<font color=\"#000099\"><font color=\"#990099\">&lt;style type=<font color=\"#0000ff\">\"text/css\"</font>&gt;</font></font>(.*)<font color=\"#000099\"><font color=\"#990099\">&lt;/style&gt;</font></font>!siU",array(&$this,"CSS"),$string);
		
		
		$Code = nl2br($string);
		
		//> Hier könnt ihr den Header und Footer für HTML editieren.
		$Header = "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"".$this->Code_TableWidth."\"><tr><td width=\"".$this->Code_TableWidth."\" valgin=\"top\">"
				 ."<div style=\"overflow: auto; height: ".(37+(count($Line)*16))."; width: 100%;\">"
				 ."<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" style=\"BORDER: 1px SOLID ".$this->Code_BGColor1.";\" width=\"100%\">"
				 ."<tr><td colspan=\"3\" style=\"FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif;FONT-SIZE:13px;FONT-WEIGHT:BOLD;COLOR:".$this->Code_FontColor.";BACKGROUND-COLOR:".$this->Code_BGColor1.";\">&nbsp;HTML-Code</td></tr>"
				 ."<tr bgcolor=\"".$this->Code_BGColor2."\"><td style=\"COLOR:".$this->Code_FontColor.";BACKGROUND-COLOR:".$this->Code_BGColor1.";\" align=\"center\" width=\"20\" valign=\"top\"><code>"
				 .$Lines
				 ."</code></td><td width=\"5\">&nbsp;</td><td valign=\"top\" nowrap><code>";
		$Footer = "</code></td></tr></table></div></td></tr></table>";
		
		return $Header.trim($Code).$Footer; 
		
	}
?>

3. Warum wird die funktion preg_replace_callback() so gut wie vermieden gibs da nachteile gegenüber preg_replace()?

So ansonsten war das schon alles hoffe mir kann jemand helfen :)

MfG Funjoy
 
Zum 1:
zum Einen dürfte es schneller sein, wenn du Sachen, wofür du keine RegExp brauchst, mit str_replace() ersetzt. Ob's mit Arrays schneller geht, kann ich nicht sagen... ich würd sie aber wegen der besseren Übersicht verwenden.

zum 2: du verwendest dort, wenn ich richtig gezählt habe , 42 mal preg_replace[_callback()].... bei 100 Zeilen macht das 4200 Aufrufe... das kann schon eine Weile dauern.

Anstatt die Sache am Anfang zu exploden, würd ich den Text als Ganzes erstmal highlighten(schliesslich ist die HTML-Knotenstruktur ohnehin nicht an Zeilen gebunden)... und erst am Ende exploden, um die Zeilennummern dazuzupacken.

zu 3: Vielleicht, weil preg_replace() mit der Flag e den selben Zweck erfüllt.
 
Du musst nicht für jede Ersetzung ein eigenes preg_replace machen,
schreib doch bitte ein Array mit dem jeweiligen Regex und er Ersetzung
und mache dann EINEN preg_replace mit dem String. Der durchläuft
dann alle Muster mit ihrer Ersetzung und sparst PCZeit.
 
Danke euch beiden!

Beim highlight werde ich mal die funktion str_replace() testen. Ich mache auch alle Daten nun in ein Array weill es auch besser ausschaut ;)

So das mit den Flag e habe ich gestern auch schon rausbekommen doch nun habe ich ein kleines Problem undzwar wie kann ich die funktionen ansprechen die in einer klasse sind also so geht es ja nicht preg_replace("%\[BB\](.+)\[\/BB\]%seU","$this->funktion($1)",$string);

bei preg_replace_callback() konnte ich sie so ansprechen array(&$this,"funktion") gibs da, für die e Flag Methode auch was?

MfG Funjoy
 
Hallo,

Hat sich erledigt nun weiß ich wie das geht. Habe einfach mal in der BBCode Klasse von phpBB 2 geschaut und da wird es so gemacht (funktioniert auch) :

PHP:
<?php 
$string = preg_replace("%\[BB\](.+)\[/BB\]%ieU","\$this->kursiv('\$1','\$1')",$string); 
?>

MfG Funjoy
 
Zurück