Version 1.0, August 29, 2004
C00L Code highlighter
When working with your favorite editor you probably have something called syntax highlighting. It colors the different parts so that it's easier to read.
PHP has two functions for highlighting PHP code. highlight_string() and highlight_file(). They are however, quite limited.
What it does
The standard PHP functions can only be used for complete PHP scripts that start and with <? and end with ?>. They also automatically generate outputs bad, deprecated HTML with ugly <font> tags. The colors of the highlighting cannot be defined and you will end up with <code> tags around the output, whether you like it or not.
This script will highlight any code, be it PHP, JavaScript, CSS, HTML or Java as long as it has a slightly similar syntax it will be highlighted (Some better than others).
This script overcomes the shortcomings of the original functions. Here's a short list:
- 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
Where to use it
You can use it for posting pieces of code on your website, or integrate it in a UBB parser that can be used for a weblog, a forum or a CMS or anywhere else you need code to be published on the internet.
You can also use it for exporting colored code to a presentation, PDF or other document. Normal editors don't copy the syntax highlighting when you copy code to another application. If you parse it through the code highlighter and copy it from your browser the colors will be copied.
Show me the code!
This is the core of the script, a single function named highlight_code() that parses code.
This colorful example was also parsed using this code highlighter.
>
<?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($code, 0, 2) != '<?') {
$code = "<?\n".$code."\n?>";
$add_tags = true;
}
$code = highlight_string($code, true);
// Remove the first "<code>" tag from "$code" (if any)
if(substr($code, 0, 6) == '<code>') {
$code = substr($code, 6, (strlen($code) - 13));
}
// Replacement-map to replace deprecated "<font>" tag with "<span>"
$xhtml_convmap = array(
'<font' => '<span',
'</font>' => '</span>',
'style="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($code, 25, (strlen($code) -33));
//strip the PHP tags if they were added by the script
if($add_tags) {
$code = substr($code, 0, 26).substr($code, 36, (strlen($code) - 74));
}
return $code;
}
?>
Some things you might want to know
When submitting code with a <form> you need to use stripslashes() before parsing.
When displaying the result in a <textarea> you need to use htmlspecialchars() on the output.
Use file_get_contents() to parse entire files of code. If you PHP built-in function highlight_file() it only highlights PHP files and not other languages.
If it's not working
It can happen that in the beginning/end of the output some colors and HTML code is not good. This happens when the original colors do not match the ones in the script. Different PHP versions use slightly different colors.
To fix this, replace the colors in the script (#000000, #0000BB, #007700, #FF8000, #DD0000) with the ones in your faulty output. This will fix the problem.
Using long colors like #00700 instead of #070 will result in the same problems. You can fix it by changing some of the substr() function's parameters (change 26 into 29).
Additionally, using extremely short code samples can result in invalid HTML output, this should not be an issue when using real life coding examples.
Download the script
The downloadable script contains a fully functional HTML page that is the source of the Code highligter demo.
Rename the .phps file to .php so that it will be parsed.