PHP Highlighting geht, aber normaler Text nicht

Du hast Probleme beim Einbau oder bei der Benutzung eines Mods? In diesem Forum bist du richtig.
Forumsregeln
phpBB 2.0 hat das Ende seiner Lebenszeit überschritten
phpBB 2.0 wird nicht mehr aktiv unterstützt. Insbesondere werden - auch bei Sicherheitslücken - keine Patches mehr bereitgestellt. Der Einsatz von phpBB 2.0 erfolgt daher auf eigene Gefahr. Wir empfehlen einen Umstieg auf phpBB 3.0, welches aktiv weiterentwickelt wird und für welches regelmäßig Updates zur Verfügung gestellt werden.
Antworten
daryl
Mitglied
Beiträge: 2
Registriert: 26.11.2005 17:40

PHP Highlighting geht, aber normaler Text nicht

Beitrag 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?
Antworten

Zurück zu „phpBB 2.0: Mod Support“