User:Pyramide/PHP Syntaxhighlighter for MediaWiki

This is a simple PHP syntaxhighlighter for MediaWiki (inspired by the Syntax Highlighting Extension). It only supports PHP but doesn't require any external tools. The formatting will look exactly like the code you see on this page.

To install, just copy&paste the following code and save it as extensions/phpExtension.php: setHook("PHP", "phpSyntaxHighlight"); }

function phpSyntaxHighlight($input, $argv) { //prepare input $input = trim($input);

//if we dont have php tags, add them $strip_php = false; if(stristr($input, '<'.'?php') === false) { $input = "<"."?php\n$input\n?".">"; $strip_php = true; }

$output = highlight_string($input, true); if($strip_php) { $output = str_replace(array('&lt;?php ', '?&gt;'), '', $output); }

//filter out unnescessary stuff $output = str_replace(array(' ', "\r", "\n"), '', $output); $output = " $output "; return $output; } ?>

Then open LocalSettings.php and add this line: require_once("extensions/phpExtension.php");

The extension should be ready to use now. Just wrap your PHP code inside ... (you dont even need the leading space like for normale code sections).