User:Marksw/PHPHighlight

 
A proposal to move this page to MediaWiki.org was rejected.

PHPHighlight is a PHP syntax highlighting extension for MediaWiki

Syntax

edit

<php>php code</php>


Example

edit
<php>
// Example 1
$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
</php>

Result

edit

This will give something like the following (though not exactly):


// Example 1
$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo
$pieces[0]; // piece1
echo $pieces[1]; // piece2


Source

edit
<?php
/*
** PHPHighlight.php
** PHP syntax highlighting MediaWiki Extension
**
** Version 1.0 by Mark Sweeting 2005/09/05
**     http://meta.wikimedia.org/wiki/User:Marksw/PHPHighlight
*/

$wgExtensionFunctions[] = "wfPHPHighlightExtension";

function wfPHPHighlightExtension() {
   global $wgParser;
   $wgParser->setHook( "php", "renderPHP" );
}

function renderPHP( $input="", $argv=array() )
{
   // need to add the PHP tags so that the highlight_string()
   // method works.
   $input = '<?php' . $input .  '?>' . "\n";
   $output = highlight_string($input, true);
   // now remove the outer PHP tags
   $output = str_replace (array('<?php',
                                '?>',
                                '<code>',
                                '</code>'),
                          '',
                          $output);

   return '<pre>' . preg_replace('|<br ?/?>|i', "\n", trim($output)) . '</pre>';
}
?>

Installing the extension

edit

Copy the code above into a file called PHPHighlight.php and save this into your wiki extensions directory.

Then add the following line to the end of your LocalSettings.php file:

include("extensions/PHPHighlight.php");

and you are ready to go.

edit

If you would like a syntax highlighter for many other languages (including PHP) then see GeSHiHighlight