NEW QUESTION: Is there a way to replace all newlines with linebreaks (nl2br) everything outside [ php ], [/ php ] and [ code ], [/ code ]?
Here's the highligh-class:
Code: Select all
<?php
class highlighter {
var $code; // code to highlight
var $adjust; // add <?php if needed?
var $line_numbers; // show linenumbers?
var $highlight_method; // highlight_string / highlight_file
var $title; // title for code
var $isphp; // php or other code?
var $linebreaks; // show linebreaks?
############## HIGHLIGHTER - initiation
function highlighter ($br = false, $method = 'highlight_string') {
$this->linebreaks = $br;
$this->highlight_method = $method;
// set defaults
$this->adjust = true;
$this->line_numbers = true;
$this->title = "PHP Code";
$this->isphp = true;
}
############## SET_CONFIG - set configuration
function set_config ($property, $value) {
if (isset ($this->$property)) {
$this->$property = $value;
// if $isphp is changed
if ($property == 'isphp' && $value == false) $this->title = 'Code';
return true;
} else {
return false;
}
}
############## SET_CODE - sets the code to highlight
function set ($code) {
$this->code = $code; // decode special characters into real code
}
############## CLEAR - empty code
function clear () {
$this->code = '';
}
############## MAKE_CSS - create html/css code
function make_css ($code) {
if (PHP_VERSION >= 5) { // PHP 5 highlighting
$code = preg_replace (
'{(ї\w_]+)(\s*</span>)'.
'(\s*<span\s+style="color: '.ini_get ('highlight.keyword').'">\s*\()}m',
"<a class='function' href='http://www.php.net/$1'>$1</a>$2$3",
$code);
} else { // old highlighting
$code = preg_replace (
'{(ї\w_]+)(\s*</font>)'.
'(\s*<font\s+color="'.ini_get ('highlight.keyword').'">\s*\()}m',
"<a class='function' href='http://www.php.net/$1'>$1</a>$2$3",
$code);
}
$colorsїini_get ('highlight.bg')] = 'background';
$colorsїini_get ('highlight.comment')] = 'comment';
$colorsїini_get ('highlight.default')] = 'default';
$colorsїini_get ('highlight.html')] = 'html';
$colorsїini_get ('highlight.keyword')] = 'keyword';
$colorsїini_get ('highlight.string')] = 'string';
foreach ($colors as $color=>$class) {
$code = str_replace ('<span style="color: '.$color.'">', "<span class='$class'>", $code); // PHP 5 highlighting
$code = str_replace ('<font color="'.$color.'">', "<span class='$class'>", $code); // old highlighting
}
return str_replace ('</font>', '</span>', $code);
}
############## PROCESS - processes code and creates output
function process () {
$code = $this->code;
$hlfunc = $this->highlight_method;
if ($this->isphp) { // highlight if php
if ($this->adjust && !strstr ($code, '<?php') && !strstr ($code, '<?')) $code = "<?php
$code
?>";
$code = $hlfunc ($code, true);
$code = $this->make_css ($code);
$lines = count (explode ('<br />', $code));
if (!$this->linebreaks) $code = str_replace ('<br />', '', $code); // delete linebreaks if requested
$output = @file_get_contents ('tpl/php.tpl');
} else { // not php
$code = "<span class='keyword'>".nl2br (htmlentities ($code))."</span>";
$code = str_replace (' ', ' &nbsp;', $code); // 2 spaces > 1 space + 1 nbsp
$code = str_replace (chr(9), '&nbsp; &nbsp; ', $code); // Tab > 4 spaces
$lines = count (explode ('<br />', $code));
if (!$this->linebreaks) $code = str_replace ('<br />', '', $code); // delete linebreaks if requested
$output = @file_get_contents ('tpl/code.tpl');
}
for ($i = 1; $i < ($lines + 1); $i++) {
if($this->line_numbers) {
$rownumbers .= "$i<br />";
}
}
$output = str_replace ('{%title%}', $this->title, $output); // title
$output = str_replace ('{%rownumbers%}', $rownumbers, $output); // rownumbers
$output = str_replace ('{%code%}', $code, $output); // actual code
$output = str_replace ('{%numlines%}', $lines, $output); // number of lines
return $output;
}
}
Formatting-function:
Code: Select all
<?php
########## function format ($string, $level, $ add linebreaks) ##########
function format ($string, $lvl = 5, $br = false) {
$string = stripslashes ($string);
########## change html to entities if level is under 4
if ($lvl < 4) $string = htmlentities ($string);
########## completly stip html tags if level is under 3
if ($lvl < 3) $string = strip_tags ($string);
########## no tags or formatting allowed if level is under 2
if ($lvl < 2) {
$string = str_replace (chr(91), '[', $string); // ї
$string = str_replace (chr(93), ']', $string); // ]
if ($br) $string = nl2br ($string); // replace newlines with linebreaks
return $string; // skip formatting
}
if ((strpos ($string, 'ї') === FALSE) || (strpos ($string, ']') === FALSE)) { // if no code-blocks was found
return $string; // skip formatting
} // if code-blocks was found
######## code ########
// function which highlights code and returns formatted output
if (!function_exists ('highlight')) { // declare functions if they doesn't already exist
function highlight ($code) {
@include_once ('mods/highlight.php');
$hlight = New highlighter(!$br);
$hlight->set ($codeї2]);
return $hlight->process ();
}
function highlight_title ($code) {
@include_once ('mods/highlight.php');
$hlight = New highlighter(!$br);
$hlight->set_config ('title', $codeї1]);
$hlight->set ($codeї3]);
return $hlight->process ();
}
function hl_code ($code) {
@include_once ('mods/highlight.php');
$hlight = New highlighter(!$br);
$hlight->set_config ('isphp', false);
$hlight->set ($codeї2]);
return $hlight->process ();
}
function hl_code_title ($code) {
@include_once ('mods/highlight.php');
$hlight = New highlighter(!$br);
$hlight->set_config ('isphp', false);
$hlight->set_config ('title', $codeї1]);
$hlight->set ($codeї3]);
return $hlight->process ();
}
} // declare functions if they doesn't already exist
// php highlighting
$string = preg_replace_callback ("#\ї php=(.*?)\](\s*)(.*?)(\s*)\ї/php\]#si", "highlight_title", $string); ######### note ########### added a space due to phpBB bug ( forget this line) #########
$string = preg_replace_callback ("#\їphp\](\s*)(.*?)(\s*)\ї/php\]#si", "highlight", $string);
// code highlighting
$string = preg_replace_callback ("#\їcode=(.*?)\](\s*)(.*?)(\s*)\ї/code\]#si", "hl_code_title", $string);
$string = preg_replace_callback ("#\їcode\](\s*)(.*?)(\s*)\ї/code\]#si", "hl_code", $string);
####### /code ########
################### /replace ####################
if ($br) $string = nl2br ($string); // replace newlines with linebreaks
### return formatted string
return $string;
} ######## /function format ($string, $level) ##########
?>
the string sent is formatted with these arguments:
Code: Select all
$postї'body'] = format ($postї'body'], 3, true);
Example of string sent:
Code: Select all
<b>html coding - bold</b>
<a href='{%id%}'>test</a>
їb]testing....ї/b]
їurl=http://mgs3.vigge.net]url to mgs3-coreї/url]
їphp]
<?php
echo "testing";
?>
ї/php]
The code.tpl file contains:
Code: Select all
<div class='box'>
<div class='header'>{%title%} ({%numlines%} lines) ::</div>
<div class='num'><code>
{%rownumbers%}
</code></div>
<div class='php'>
{%code%}
</div>
</div>
Currently ouputs:
Code: Select all
<b>html coding - bold</b><br />
<a href='{%id%}'>test</a><br />
<b>testing....</b><br />
<a href='http://mgs3.vigge.net' rel='ext'>url to mgs3-core</a><br />
<div class='box'><br />
<div class='header'>PHP Code (3 lines) ::</div><br />
<div class='num'><code><br />
1<br />2<br />3<br /><br />
</code></div><br />
<div class='php'><br />
<code><span class='html'><br />
<span class='default'><?php<br />
<br /></span><span class='keyword'>echo </span><span class='string'>"testing"</span><span class='keyword'>;<br />
<br /></span><span class='default'>?></span><br />
</span><br />
</code><br />
</div><br />
As you can see, alot of <br />s are added where they shouldn't.
If you want a live example, check
http://vigge.ath.cx/p/board/ out.
Phew, lots of copying and pasting, I hope you understand what i've posted.
Thanks in advance.
EDIT: feel free to try the posting out, if you need any example output, etc.
(I will delete the entries when the board's gonna be available to the public anyway

)