Syntax Highlighting Class for PHP
This is an modified and extended PHP syntax highlighting class originally published in a blog post called Generic Syntax Highlighting with Regular Expressions by Dominic Szablewski.
You may download the source code and the CSS file from GitHub.
This class is used for syntax highlighting on this website too.
Changes are:
special functions for HTML, CSS and PHP
added a bunch of keywords
sorted keywords alphabetically
added rule for PHP start and end tags
Usage:
$output = SyntaxHighlight::process($input [, $lang]);
where the optional $lang
is one of
"html"
"php"
"css"
Example:
<?php
include ("SyntaxHighlight.php");
$html = '
<html lang="en">
<body>
<?php echo $content; ?>
</body>
</html>
';
$colored = SyntaxHighlight::process($html, "html");
$even_more_colored = SyntaxHighlight::process($colored, "php");
?>
$colored
:
<span class="html_tag"><html <span class="html_attr">lang</span>=<span class="html_data">"en"</span><span class="html_tag">></span>
<span class="html_tag"><body</span><span class="html_tag">></span>
<?php echo content; ?>
<span class="html_tag"></body</span><span class="html_tag">></span>
<span class="html_tag"></html</span><span class="html_tag">></span>
$even_more_colored
:
<span class="html_tag"><html <span class="html_attr">lang</span>=<span class="html_data">"en"</span><span class="html_tag">></span>
<span class="html_tag"><body</span><span class="html_tag">></span>
<span class="D"><?php</span> <span class="K">echo</span> content; <span class="D">?></span>
<span class="html_tag"></body</span><span class="html_tag">></span>
<span class="html_tag"></html</span><span class="html_tag">></span>
Have fun ...