Seite 1 von 1

PHP Highlighting geht, aber normaler Text nicht

Verfasst: 10.06.2006 13:56
von daryl
Ich habe eine PHP Highlighter-Funktion eingebaut, die ich auf phphacks.com gefunden habe. Nun wird der PHP-Code auc hschön dargestellt, allerdings wird im normalen Text jedes Leerzeichen durch   ersetzt - und somit bricht der Text nicht um. Stattdessen habe ic hsehr lange Zeilen und das sieht nicht so toll aus.
Hier die Funktion:

Code: Alles auswählen

function bbencode_second_pass_php( $text, $uid, $bbcode_tpl )
{
	global $lang;

   $text = undo_htmlspecialchars($text);
   If  ( eregi( "\\<\\?...", $text) &&  eregi( "\\?\\>", $text) )
   {
      ob_start();
      highlight_string($text);
      //highlighted text
      $text = " " . ob_get_contents();
      ob_end_clean();
   }
   else
   {
      if ( !eregi( "\\<\\?...", $text) &&  !eregi( "\\?\\>", $text) )
      {
         $text = str_replace( "[php:1:$uid]", "[php:1:$uid]<?", $text);
         $text = str_replace( "[/php:1:$uid]", "?>[/php:1:$uid]", $text);
      }
      else if ( !eregi( "\\<\\?...", $text) )
      {
         $text = str_replace( "[php:1:$uid]", "[php:1:$uid]<?", $text);
      }
      else if ( !eregi( "\\?\\>", $text) )
      {
         $text = str_replace( "[/php:1:$uid]", "?>[/php:1:$uid]", $text);
      }
      ob_start();
      highlight_string($text);
      //highlighted text
      $colorphptext = " " . ob_get_contents();
      ob_end_clean();

      $pos1 = strpos ($colorphptext,"<?");
      $pos2 = strrpos ($colorphptext,"?>");

      $text = substr($colorphptext, 0, $pos1).substr($colorphptext, $pos1+5, $pos2-($pos1+5)).substr($colorphptext, $pos2+5);
   }

	$php_start_html = $bbcode_tpl['php_open'];
	$php_end_html =  $bbcode_tpl['php_close'];

	// First, do all the 1st-level matches. These need an htmlspecialchars() run,
	// so they have to be handled differently.
	$php_match_count = preg_match_all("#\[php:1:$uid\](.*?)\[/php:1:$uid\]#si", $text, $php_matches);

	for ($i = 0; $i < $php_match_count; $i++)
	{
		$before_replace = $php_matches[1][$i];
		$after_replace = $php_matches[1][$i];

		// Replace 2 spaces with "&nbsp; " so non-tabbed code indents without making huge long lines.
		$after_replace = str_replace("  ", "&nbsp; ", $after_replace);
		// now Replace 2 spaces with " &nbsp;" to catch odd #s of spaces.
		$after_replace = str_replace("  ", " &nbsp;", $after_replace);

		// Replace tabs with "&nbsp; &nbsp;" so tabbed code indents sorta right without making huge long lines.
		$after_replace = str_replace("\t", "&nbsp; &nbsp;", $after_replace);

		$str_to_match = "[php:1:$uid]" . $before_replace . "[/php:1:$uid]";

		$replacement = $php_start_html;
		$replacement .= $after_replace;
		$replacement .= $php_end_html;
    
		$text = str_replace($str_to_match, $replacement, $text);
	}

	  // Now, do all the non-first-level matches. These are simple.
	  $text = str_replace("[php:$uid]", $php_start_html, $text);
	  $text = str_replace("[/php:$uid]", $php_end_html, $text);

   return "$text";
}
Irgendwie sieht der alles als PHP-Code an - im ersten if-Zweig ersetzt er dann die Leerzeichen (durch die Funktion highlight_string()). Kann man das irgendwie ändern, dass der nur die Abschnitte berücksichtigt, die zwischen [PHP] und [/PHP] sind?