I wrote this function when I was writing my PHP quiz.
mixed highlight_code( string $code, bool $class, bool $return );
More verbose
highlight_string. This will either echo or return the code (returns by default). The $class argument will allow you to insert a CSS class in the highlighted PHP's
<code> tags. This is useful if you want to highlight code in both a block element and inline elements.
PHP Code:
<?php
function highlight_code($code, $class=false, $return=true)
{
if ( empty($code) ) return false;
$highlight = '';
if ( version_compare(PHP_VERSION, '4.2.0', '<') )
{
ob_start(); // start output buffering to capture contents of highlight
highlight_string($code);
$highlight = ob_get_contents(); // capture output
ob_end_clean(); // clear buffer cleanly
}
else
{
$highlight = highlight_string($code, true);
}
if ( $class )
$highlight=preg_replace('/<code>/i',"<code class=\"${class}\">", $highlight);
if ( $return )
return $highlight;
else
echo $highlight;
}
?>
Created by Curtis Dyer