<?php 

/*
    Extends the PHP function highlight_string()
    - outputs valid (X)HTML
    - customizable colors
    - strips <code> tags
    - parses all PHP code, even without start/end tags
    - can be used for many other languages
*/

function highlight_code($code) {

    
define(COLOR_DEFAULT'000');
    
define(COLOR_FUNCTION'00b'); //also for variables, numbers and constants
    
define(COLOR_KEYWORD'070');
    
define(COLOR_COMMENT'800080');
    
define(COLOR_STRING'd00');

    
// Check it if code starts with PHP tags, if not: add them.
    
if(substr($code02) != '<?') {
        
$code "<?\n".$code."\n?>";
        
$add_tags true;
    }
    
    
$code highlight_string($codetrue);

    
// Remove the first "<code>" tag from "$code" (if any)
    
if(substr($code06) == '<code>') {
       
$code substr($code6, (strlen($code) - 13));
    }

    
// Replacement-map to replace deprecated "<font>" tag with "<span>"
    
$xhtml_convmap = array(
       
'<font' => '<span',
       
'</font>' => '</span>',
       
'color="' => 'style="color:',
       
'<br />' => '<br/>',
       
'#000000">' => '#'.COLOR_DEFAULT.'">',
       
'#0000BB">' => '#'.COLOR_FUNCTION.'">',
       
'#007700">' => '#'.COLOR_KEYWORD.'">',
       
'#FF8000">' => '#'.COLOR_COMMENT.'">',
       
'#DD0000">' => '#'.COLOR_STRING.'">'
    
);

    
// Replace "<font>" tags with "<span>" tags, to generate a valid XHTML code
    
$code strtr($code$xhtml_convmap);

    
//strip default color (black) tags
    
$code substr($code25, (strlen($code) -33));

    
//strip the PHP tags if they were added by the script
    
if($add_tags) {
        
        
$code substr($code026).substr($code36, (strlen($code) - 74));
    }

    return 
$code;
}

?>