Skip to content

Instantly share code, notes, and snippets.

@tot-ra
Created March 29, 2013 09:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tot-ra/5269960 to your computer and use it in GitHub Desktop.
Save tot-ra/5269960 to your computer and use it in GitHub Desktop.
Short PHP code highlighting function
function php_highlight($source, $classes = true) {
$r1 = $r2 = '##';
// adds required PHP tags (at least with vers. 5.0.5 this is required)
if (strpos($source, ' ?>') === false) // xml is not THAT important ;-)
{
$source = "<?php " . $source . " ?>";
$r1 = '#&lt;\?.*?(php)?.*?&nbsp;#s';
$r2 = '#\?&gt;#s';
}
elseif (strpos($source, '<? ') !== false)
{
$r1 = '--';
$source = str_replace('<? ', '<?php ', $source);
}
$source = highlight_string($source, true);
if ($r1 == '--') $source = preg_replace('#(&lt;\?.*?)(php)?(.*?&nbsp;)#s', '\\1\\3', $source);
$source = preg_replace (array ( '/.*<code>\s*<span style="color: #000000">/', //
'#</span>\s*</code>#', // <code><span black>
$r1, $r2, // php tags
'/<span[^>]*><\/span>/' // empty spans
),'',$source);
if ($classes) $source = str_replace(
array('style="color: #0000BB"', 'style="color: #007700"',
'style="color: #DD0000"', 'style="color: #FF8000"'),
array('class="default"', 'class="keyword"',
'class="string"', 'class="comment"',), $source);
return $source;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment