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.
Changes are:
- use
preg_replace_callback
for PHP7 compatibility - special functions for HTML, CSS and PHP
- added a bunch of keywords
- sorted keywords alphabetically
- added rule for PHP start end end tags
Usage: Include the provided CSS file in your HTML header and do something like this:
$output = SyntaxHighlight::process($input [, $lang]);
where the optional $lang
is one of
- "html"
- "php"
- "css" If no language is specified genericg syntax highligting takes place.
Example:
<?php
require_once "SyntaxHighlight.php";
$html = <<<HTML
<html>
<body>
<?php echo content; ?>
</body>
</html>
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>