<?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 'em.
    
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;
}
    

//process POST data
if(!empty($_POST['code'])) {
    
$html_code highlight_code(stripslashes($_POST['code']));
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xml:lang="en">
<head>
<head>
<title>Code highlighter</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<h1>Code highlighter</h1>

<p>Applies syntax highlighting to all your code!</p>

<p><a href="http://www.brainerror.net/scripts_php_highlight.php">Click here</a> for more info on how to use the script.</p>

<form method="post" action="">
<label for="textCode">Enter your code:</label><br/>
<textarea name="code" id="textCode" style="width:600px; height:320px;"><?php echo(htmlspecialchars($html_code)); ?></textarea><br/>
<input type="submit" value="Highlight!" />
</form>

<code>
<?php echo($html_code); ?>
</code>

<p><strong><a href="highlight_code.phps">Download this file's source code</a></strong></p>

</body>
</html>