Created
March 29, 2013 09:56
-
-
Save tot-ra/5269960 to your computer and use it in GitHub Desktop.
Short PHP code highlighting function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = '#<\?.*?(php)?.*? #s'; | |
$r2 = '#\?>#s'; | |
} | |
elseif (strpos($source, '<? ') !== false) | |
{ | |
$r1 = '--'; | |
$source = str_replace('<? ', '<?php ', $source); | |
} | |
$source = highlight_string($source, true); | |
if ($r1 == '--') $source = preg_replace('#(<\?.*?)(php)?(.*? )#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