limi.eu

Syntax Highlighting Class for PHP

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:

Usage:

$output = SyntaxHighlight::process($input [, $lang]);

where the optional $lang is one of

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">&lt;html <span class="html_attr">lang</span>=<span class="html_data">"en"</span><span class="html_tag">&gt;</span>
<span class="html_tag">&lt;body</span><span class="html_tag">&gt;</span>
    &lt;?php echo content; ?&gt;
<span class="html_tag">&lt;/body</span><span class="html_tag">&gt;</span>
<span class="html_tag">&lt;/html</span><span class="html_tag">&gt;</span>

$even_more_colored:

<span class="html_tag">&lt;html <span class="html_attr">lang</span>=<span class="html_data">"en"</span><span class="html_tag">&gt;</span>
<span class="html_tag">&lt;body</span><span class="html_tag">&gt;</span>
    <span class="D">&lt;?php</span> <span class="K">echo</span> content; <span class="D">?&gt;</span>
<span class="html_tag">&lt;/body</span><span class="html_tag">&gt;</span>
<span class="html_tag">&lt;/html</span><span class="html_tag">&gt;</span>

Have fun ...