PHP Syntax Highlighter BBCode
Verfasst: 04.05.2003 23:45
Hi, ich hab da kürzlich einen Mod gefunden. Jetzt möcht ich die alte "Code"-Funktion mit dieser hier ersetzten. Leider bin ich kein PHP-Ass und weiß demzufolge nicht, was ich entsprechend abändern muss. Es dürften aber nur Kleinigkeiten sein. Zur Info, ich hab momentan das PHPBB 2.0.4
for posting code (HTML, PHP, C etc etc) in your posts.
$text = bbencode_second_pass_code($text, $uid, $bbcode_tpl);
#
#-----[ ACTION: ADD BELOW]------------------------------------------
#
// PHP MOD
// [PHP] and [/PHP] for posting PHP code in your posts.
$text = bbencode_second_pass_php($text, $uid, $bbcode_tpl);
#
#-----[ ACTION: FIND]------------------------------------------
#
// for posting code (HTML, PHP, C etc etc) in your posts.
$text = bbencode_first_pass_pda($text, $uid, '', '', true, '');
#
#-----[ ACTION: ADD BELOW]------------------------------------------
#
// PHP MOD
// [PHP] and [/PHP] for posting PHP code in your posts.
$text = bbencode_first_pass_pda($text, $uid, '[php]', '[/php]', '', true, '');
#
#-----[ ACTION: FIND]------------------------------------------
#
} // bbencode_second_pass_code()
#
#-----[ ACTION: ADD BELOW]------------------------------------------
#
/**
* PHP MOD
* Does second-pass bbencoding of the [php] tags. This includes
* running htmlspecialchars() over the text contained between
* any pair of [php] tags that are at the first level of
* nesting. Tags at the first level of nesting are indicated
* by this format: [php:1:$uid] ... [/php:1:$uid]
* Other tags are in this format: [php:$uid] ... [/php:$uid]
*
* Original code/function by phpBB Group
* Modified by JW Frazier / Fubonis < php@fubonis.com >
*/
function bbencode_second_pass_php($text, $uid, $bbcode_tpl)
{
global $lang;
$html_entities_match = array("#<#", "#>#");
$html_entities_replace = array("<", ">");
$code_start_html = $bbcode_tpl['php_open'];
$code_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.
$match_count = preg_match_all("#\[php:1:$uid\](.*?)\[/php:1:$uid\]#si", $text, $matches);
for ($i = 0; $i < $match_count; $i++)
{
$before_replace = $matches[1][$i];
$after_replace = $matches[1][$i];
$after_replace = preg_replace($html_entities_match, $html_entities_replace, $after_replace);
// Replace 2 spaces with " " so non-tabbed code indents without making huge long lines.
$after_replace = str_replace(" ", " ", $after_replace);
// now Replace 2 spaces with " " to catch odd #s of spaces.
$after_replace = str_replace(" ", " ", $after_replace);
// Replace tabs with " " so tabbed code indents sorta right without making huge long lines.
$after_replace = str_replace("\t", " ", $after_replace);
$str_to_match = "[php:1:$uid]" . $before_replace . "[/php:1:$uid]";
$replacement = $code_start_html;
$after_replace = str_replace('<', '<', $after_replace);
$after_replace = str_replace('>', '>', $after_replace);
ob_start();
highlight_string($after_replace);
$after_replace = ob_get_contents();
ob_end_clean();
$replacement .= $after_replace;
$replacement .= $code_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]", $code_start_html, $text);
$text = str_replace("[/php:$uid]", $code_end_html, $text);
$text = str_replace(' ', ' ', $text);
$text = str_replace('&</font><font color="#0000CC">nbsp</font><font color="#006600">;', ' ', $text);
$text = str_replace('&</font><font color="#0000BB">nbsp</font><font color="#007700">;', ' ', $text);
return $text;
} // bbencode_second_pass_php()
#
#-----[ ACTION: CLOSE /includes/bbcode.php]------------------------------------------
#
#
#-----[ ACTION: OPEN /templates/YOUR_TEMPLATE_NAME/bbcode.tpl]------------------------------------------
#
#
#-----[ ACTION: FIND]------------------------------------------
#
<!-- END email -->
#
#-----[ ACTION: ADD BELOW]------------------------------------------
#
<!-- BEGIN php_open -->
</span>
<table border="0" align="center" width="90%" cellpadding="3" cellspacing="1">
<tr>
<td><span class="genmed"><b>{L_PHP}:</b></span></td>
</tr>
<tr>
<td class="code">
<!-- END php_open -->
<!-- BEGIN php_close -->
</td>
</tr>
</table>
<span class="postbody">
<!-- END php_close -->
#
#-----[ ACTION: CLOSE /templates/YOUR_TEMPLATE_NAME/bbcode.tpl]------------------------------------------
#
#
#-----[ ACTION: OPEN /templates/YOUR_TEMPLATE_NAME/posting_body.tpl]------------------------------------------
#
#
#-----[ ACTION: FIND]------------------------------------------
#
f_help = "{L_BBCODE_F_HELP}";
#
#-----[ ACTION: ADD BELOW]------------------------------------------
#
h_help = "{L_BBCODE_H_HELP}"; <!-- PHP MOD //-->
#
#-----[ ACTION: FIND]------------------------------------------
#
bbtags = new Array('','','','','','','[quote]','[/quote]','','
#
#-----[ ACTION: REPLACE WITH]------------------------------------------
#
bbtags = new Array('','','','','','','[quote]','[/quote]','','
#
#-----[ ACTION: FIND]------------------------------------------
#
<td><span class="genmed">
<input type="button" class="button" accesskey="w" name="addbbcode16" value="URL" style="text-decoration: underline; width: 40px" onClick="bbstyle(16)" onMouseOver="helpline('w')" />
</span></td>
#
#-----[ ACTION: ADD BELOW]------------------------------------------
#
<td><span class="genmed">
<input type="button" class="button" accesskey="h" name="addbbcode18" value="PHP" style="width: 40px" onClick="bbstyle(18)" onMouseOver="helpline('h')" />
</span></td> <!-- PHP MOD //-->
#
#-----[ ACTION: FIND]------------------------------------------
#
<select name="addbbcode18" onChange="bbfontstyle('[color=' + this.form.addbbcode18.options[this.form.addbbcode18.selectedIndex].value + ']', '[/color]')" onMouseOver="helpline('s')">
#
#-----[ ACTION: REPLACE WITH]------------------------------------------
#
<select name="addbbcode20" onChange="bbfontstyle('[color=' + this.form.addbbcode20.options[this.form.addbbcode20.selectedIndex].value + ']', '[/color]')" onMouseOver="helpline('s')">
#
#-----[ ACTION: FIND]------------------------------------------
#
<select name="addbbcode20" onChange="bbfontstyle('[size=' + this.form.addbbcode20.options[this.form.addbbcode20.selectedIndex].value + ']', '[/size]')" onMouseOver="helpline('f')">
#
#-----[ ACTION: REPLACE WITH]------------------------------------------
#
<select name="addbbcode22" onChange="bbfontstyle('[size=' + this.form.addbbcode22.options[this.form.addbbcode22.selectedIndex].value + ']', '[/size]')" onMouseOver="helpline('f')">
#
#-----[ ACTION: CLOSE /templates/YOUR_TEMPLATE/posting_body.tpl]------------------------------------------
#
#
#-----[ ACTION: OPEN /language/lang_YOUR_LANGUAGE/lang_main.php]------------------------------------------
#
#
#-----[ ACTION: FIND]------------------------------------------
#
$lang['Code'] = "Code"; // comes before bbcode code output.
#
#-----[ ACTION: ADD BELOW]------------------------------------------
#
$lang['PHPCode'] = "PHP"; // PHP MOD
#
#-----[ ACTION: FIND]------------------------------------------
#
$lang['bbcode_f_help'] = "Font size: [size=x-small]small text[/size]";
#
#-----[ ACTION: ADD BELOW]------------------------------------------
#
$lang['bbcode_h_help'] = "PHP syntax highlighter. [php]<?php code ?>[/php] (alt+h)"; // PHP MOD
#
#-----[ ACTION: CLOSE language/lang_YOUR_LANGUAGE/lang_main.php]------------------------------------------
#
#
#-----[ ACTION: OPEN /posting.php]------------------------------------------
#
#
#-----[ ACTION: FIND]------------------------------------------
#
"L_BBCODE_F_HELP" => $lang['bbcode_f_help'],
#
#-----[ ACTION: ADD BELOW]------------------------------------------
#
"L_BBCODE_H_HELP" => $lang['bbcode_h_help'], // PHP MOD
#
#-----[ ACTION: CLOSE /posting.php]------------------------------------------
#
#
#-----[ ACTION: UPLOAD YOUR FILES]------------------------------------------
#[/code]
Code: Alles auswählen
########################################################
## Mod Title: PHP Syntax Highlighter BBCode
## Mod Version: Beta 2.0.0
## Author: JW Frazier / Fubonis < php@fubonis.com >
##
## This mod is only for phpBB2
## Tested on RC-3
## ONLY for PHP4+
##
## Description: This mod creates a new BBCode, "[php]your_code[/php]"
## It simply takes the code and uses the syntax highlighting of PHP
## and makes a pretty presentation of your code!
## To use this, your code MUST have at least the opening PHP tag
## Example: [php]<?php phpinfo(); ?>[/php] is valid
## [php]phpinfo();[/php] is not.
##
##
## Installation Level: Easy
## Installation Time: 10 Minutes
## Files To Edit: 5
## /includes/bbcode.php
## /templates/YOUR_TEMPLATE_NAME/bbcode.tpl
## /templates/YOUR_TEMPLATE_NAME/posting_body.tpl
## /language/lang_YOUR_LANGUAGE/lang_main.php
## /posting.php
########################################################
##
## Installation Notes:
##
## ALWAYS back up your files.
## Rock the open-source movement!
########################################################
#
#-----[ ACTION: OPEN /includes/bbcode.php]------------------------------------------
#
#
#-----[ ACTION: FIND]------------------------------------------
$bbcode_tpl['code_open'] = str_replace('{L_CODE}', $lang['Code'], $bbcode_tpl['code_open']);
#
#-----[ ACTION: ADD BELOW]------------------------------------------
$bbcode_tpl['php_open'] = str_replace('{L_PHP}', $lang['PHPCode'], $bbcode_tpl['php_open']); // PHP MOD
#
#-----[ ACTION: FIND]------------------------------------------
#
// [code] and $text = bbencode_second_pass_code($text, $uid, $bbcode_tpl);
#
#-----[ ACTION: ADD BELOW]------------------------------------------
#
// PHP MOD
// [PHP] and [/PHP] for posting PHP code in your posts.
$text = bbencode_second_pass_php($text, $uid, $bbcode_tpl);
#
#-----[ ACTION: FIND]------------------------------------------
#
//
Code: Alles auswählen
and $text = bbencode_first_pass_pda($text, $uid, '
Code: Alles auswählen
', '#
#-----[ ACTION: ADD BELOW]------------------------------------------
#
// PHP MOD
// [PHP] and [/PHP] for posting PHP code in your posts.
$text = bbencode_first_pass_pda($text, $uid, '[php]', '[/php]', '', true, '');
#
#-----[ ACTION: FIND]------------------------------------------
#
} // bbencode_second_pass_code()
#
#-----[ ACTION: ADD BELOW]------------------------------------------
#
/**
* PHP MOD
* Does second-pass bbencoding of the [php] tags. This includes
* running htmlspecialchars() over the text contained between
* any pair of [php] tags that are at the first level of
* nesting. Tags at the first level of nesting are indicated
* by this format: [php:1:$uid] ... [/php:1:$uid]
* Other tags are in this format: [php:$uid] ... [/php:$uid]
*
* Original code/function by phpBB Group
* Modified by JW Frazier / Fubonis < php@fubonis.com >
*/
function bbencode_second_pass_php($text, $uid, $bbcode_tpl)
{
global $lang;
$html_entities_match = array("#<#", "#>#");
$html_entities_replace = array("<", ">");
$code_start_html = $bbcode_tpl['php_open'];
$code_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.
$match_count = preg_match_all("#\[php:1:$uid\](.*?)\[/php:1:$uid\]#si", $text, $matches);
for ($i = 0; $i < $match_count; $i++)
{
$before_replace = $matches[1][$i];
$after_replace = $matches[1][$i];
$after_replace = preg_replace($html_entities_match, $html_entities_replace, $after_replace);
// Replace 2 spaces with " " so non-tabbed code indents without making huge long lines.
$after_replace = str_replace(" ", " ", $after_replace);
// now Replace 2 spaces with " " to catch odd #s of spaces.
$after_replace = str_replace(" ", " ", $after_replace);
// Replace tabs with " " so tabbed code indents sorta right without making huge long lines.
$after_replace = str_replace("\t", " ", $after_replace);
$str_to_match = "[php:1:$uid]" . $before_replace . "[/php:1:$uid]";
$replacement = $code_start_html;
$after_replace = str_replace('<', '<', $after_replace);
$after_replace = str_replace('>', '>', $after_replace);
ob_start();
highlight_string($after_replace);
$after_replace = ob_get_contents();
ob_end_clean();
$replacement .= $after_replace;
$replacement .= $code_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]", $code_start_html, $text);
$text = str_replace("[/php:$uid]", $code_end_html, $text);
$text = str_replace(' ', ' ', $text);
$text = str_replace('&</font><font color="#0000CC">nbsp</font><font color="#006600">;', ' ', $text);
$text = str_replace('&</font><font color="#0000BB">nbsp</font><font color="#007700">;', ' ', $text);
return $text;
} // bbencode_second_pass_php()
#
#-----[ ACTION: CLOSE /includes/bbcode.php]------------------------------------------
#
#
#-----[ ACTION: OPEN /templates/YOUR_TEMPLATE_NAME/bbcode.tpl]------------------------------------------
#
#
#-----[ ACTION: FIND]------------------------------------------
#
<!-- END email -->
#
#-----[ ACTION: ADD BELOW]------------------------------------------
#
<!-- BEGIN php_open -->
</span>
<table border="0" align="center" width="90%" cellpadding="3" cellspacing="1">
<tr>
<td><span class="genmed"><b>{L_PHP}:</b></span></td>
</tr>
<tr>
<td class="code">
<!-- END php_open -->
<!-- BEGIN php_close -->
</td>
</tr>
</table>
<span class="postbody">
<!-- END php_close -->
#
#-----[ ACTION: CLOSE /templates/YOUR_TEMPLATE_NAME/bbcode.tpl]------------------------------------------
#
#
#-----[ ACTION: OPEN /templates/YOUR_TEMPLATE_NAME/posting_body.tpl]------------------------------------------
#
#
#-----[ ACTION: FIND]------------------------------------------
#
f_help = "{L_BBCODE_F_HELP}";
#
#-----[ ACTION: ADD BELOW]------------------------------------------
#
h_help = "{L_BBCODE_H_HELP}"; <!-- PHP MOD //-->
#
#-----[ ACTION: FIND]------------------------------------------
#
bbtags = new Array('','','','','','','[quote]','[/quote]','
Code: Alles auswählen
','- ','
- ','
#
#-----[ ACTION: REPLACE WITH]------------------------------------------
#
bbtags = new Array('','','','','','','[quote]','[/quote]','
Code: Alles auswählen
','- ','
- ','
#
#-----[ ACTION: FIND]------------------------------------------
#
<td><span class="genmed">
<input type="button" class="button" accesskey="w" name="addbbcode16" value="URL" style="text-decoration: underline; width: 40px" onClick="bbstyle(16)" onMouseOver="helpline('w')" />
</span></td>
#
#-----[ ACTION: ADD BELOW]------------------------------------------
#
<td><span class="genmed">
<input type="button" class="button" accesskey="h" name="addbbcode18" value="PHP" style="width: 40px" onClick="bbstyle(18)" onMouseOver="helpline('h')" />
</span></td> <!-- PHP MOD //-->
#
#-----[ ACTION: FIND]------------------------------------------
#
<select name="addbbcode18" onChange="bbfontstyle('[color=' + this.form.addbbcode18.options[this.form.addbbcode18.selectedIndex].value + ']', '[/color]')" onMouseOver="helpline('s')">
#
#-----[ ACTION: REPLACE WITH]------------------------------------------
#
<select name="addbbcode20" onChange="bbfontstyle('[color=' + this.form.addbbcode20.options[this.form.addbbcode20.selectedIndex].value + ']', '[/color]')" onMouseOver="helpline('s')">
#
#-----[ ACTION: FIND]------------------------------------------
#
<select name="addbbcode20" onChange="bbfontstyle('[size=' + this.form.addbbcode20.options[this.form.addbbcode20.selectedIndex].value + ']', '[/size]')" onMouseOver="helpline('f')">
#
#-----[ ACTION: REPLACE WITH]------------------------------------------
#
<select name="addbbcode22" onChange="bbfontstyle('[size=' + this.form.addbbcode22.options[this.form.addbbcode22.selectedIndex].value + ']', '[/size]')" onMouseOver="helpline('f')">
#
#-----[ ACTION: CLOSE /templates/YOUR_TEMPLATE/posting_body.tpl]------------------------------------------
#
#
#-----[ ACTION: OPEN /language/lang_YOUR_LANGUAGE/lang_main.php]------------------------------------------
#
#
#-----[ ACTION: FIND]------------------------------------------
#
$lang['Code'] = "Code"; // comes before bbcode code output.
#
#-----[ ACTION: ADD BELOW]------------------------------------------
#
$lang['PHPCode'] = "PHP"; // PHP MOD
#
#-----[ ACTION: FIND]------------------------------------------
#
$lang['bbcode_f_help'] = "Font size: [size=x-small]small text[/size]";
#
#-----[ ACTION: ADD BELOW]------------------------------------------
#
$lang['bbcode_h_help'] = "PHP syntax highlighter. [php]<?php code ?>[/php] (alt+h)"; // PHP MOD
#
#-----[ ACTION: CLOSE language/lang_YOUR_LANGUAGE/lang_main.php]------------------------------------------
#
#
#-----[ ACTION: OPEN /posting.php]------------------------------------------
#
#
#-----[ ACTION: FIND]------------------------------------------
#
"L_BBCODE_F_HELP" => $lang['bbcode_f_help'],
#
#-----[ ACTION: ADD BELOW]------------------------------------------
#
"L_BBCODE_H_HELP" => $lang['bbcode_h_help'], // PHP MOD
#
#-----[ ACTION: CLOSE /posting.php]------------------------------------------
#
#
#-----[ ACTION: UPLOAD YOUR FILES]------------------------------------------
#[/code]