I’ve been for the last 6 month trying out how to write code for php tags that you see being used on forums and I finally figured it out. This tutorial uses GeSHi (http://qbnz.com/highlighter/) and PHP OOP style, this is not meant for the beginner.
Here’s my HighlightCode Class:
[php]<?php
include_once ‘Geshi.php’;
class HighlightCode{
public function phpCode($code, $language = 'php') {
// Set the Language to be displayed:
// Create a GeSHi object//
$geshi = new GeSHi($code, $language);
$geshi->set_header_type(GESHI_HEADER_DIV);
//$geshi->enable_classes();
$geshi->enable_line_numbers(GESHI_NO_LINE_NUMBERS);
// Format overall style
$geshi->set_overall_style('font-family: Arial, Helvetica, sans-serif; font-size: 0.8em; line-height: 1.5em; color: #000066; border: 1px solid #d0d0d0; background-color: #f0f0f0; padding-top: 5px; padding-bottom: 5px; padding-left: 20px; overflow: auto; padding-right: 20px;', false);
$geshi->set_line_style('color: #003030;', 'font-weight: bold; color: #006060; font-size: 1.2em;', true);
$geshi->set_code_style('color: #000020;', true);
// Styles for hyperlinks in the code. GESHI_LINK for default styles, GESHI_HOVER for hover style etc...
// note that classes must be enabled for this to work.
$geshi->set_link_styles(GESHI_LINK, 'color: #000060;');
$geshi->set_link_styles(GESHI_HOVER, 'background-color: #f0f000;');
return $geshi->parse_code();
}
}[/php]
Here’s my DisplayCode Class:
[php]<?php
DisplayCode class ver 1.0 beta
class DisplayCode {
protected $highlightCode;
protected $pattern;
protected $matchs = array();
protected $found;
protected $withTags = array();
protected $minusTags = array();
protected $replaceText = array();
protected $paragraphBlank = array( 0 => "<p></p>" );
protected $removePTags = NULL;
public $formattedText;
protected function highlightTheCode($string, $tagname) {
$string = '<p>' . $string . '</p>';
$this->highlightCode = new HighlightCode(); // highlight class:
$this->pattern = "/\[$tagname\](.*?)\[\/$tagname\]/is";
$this->found = preg_match_all($this->pattern, $string, $this->matchs);
if ($this->found == 0) {
return nl2br($string); // if no matchs are found just return the string.:
}
$this->withTags = array_merge_recursive($this->matchs[0]); // with the tag name in this case [PHP]:
$this->minusTags = array_merge_recursive($this->matchs[1]); // without the tag name.:
for ($i = 0; $i < count($this->minusTags); $i++) {
$this->minusTags[$i] = htmlspecialchars_decode($this->minusTags[$i], ENT_QUOTES);
$this->minusTags[$i] = $this->highlightCode->phpCode($this->minusTags[$i]); // highlight the code:
$this->minusTags[$i] = '</p>' . $this->minusTags[$i] . '<p>';
$this->replaceText[$i] = '¥~REPLACE~¥' . $i; // Filler Text in array, so nl2br can put blank lines in:
}
$this->formattedText = str_ireplace($this->withTags, $this->replaceText, $string); // replace will filler text:
$this->formattedText = nl2br($this->formattedText); // add blanks where necessary:
$this->formattedText = str_ireplace($this->replaceText, $this->minusTags, $this->formattedText); // replace filler text with formatted code:
return $this->formattedText = str_ireplace($this->paragraphBlank, $this->removePTags, $this->formattedText);
}
public function formatCode($string, $tagname = 'PHP') {
return $this->result = $this->highlightTheCode($string, $tagname);
}
}[/php]
and to execute the code:
[php] $displayCode = new DisplayCode();
$content = $page->getContent();
$content = $displayCode->formatCode($content);[/php]
I just want to warn people that I have barely tested it and it works (I have it running on my website), but I make no claims and take no responsibility of anything that might go wrong. However, I am pretty sure it is OK, for it is after the database and not before it. By that I mean it displays sanitized code, just do a CTRL-U on my website if you don’t believe me. Over the next couple of weeks I will be testing it and making any improvement on the code.