phpBB

Development Wiki

User:Pyramide/PHP Syntaxhighlighter for MediaWiki

From phpBB Development Wiki

< User:Pyramide
Revision as of 09:12, 26 December 2007 by ElorcAdelm (Talk | contribs)

letodronra 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:

<?php
$wgExtensionFunctions
[] = "phpExtension";

function 
phpExtension(){
    global 
$wgParser;
    
$wgParser->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($inputtrue);
    if(
$strip_php) {
        
$output str_replace(array('&lt;?php<br />''?&gt;'), ''$output);
    }

    
//filter out unnescessary stuff
    
$output str_replace(array('<code>''</code>'"\r""\n"), ''$output);
    
$output "<pre>$output</pre>";
    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 <php>...</php> (you dont even need the leading space like for normale code sections).