The greatest PHP Highlighting Ever!
Success. Several hours of work to reinvent the wheel … but what a wheel! I didn't like the highlight_string function because it did not allow me to use my own styles so I had to remake it. Actually I use the ouput of the original function and deface it beyond recognition.
And the output is outstanding … at least I'm very happy with it!
The challenges in recoding it!
This highlight_string function was written just for the very purpose of existing. It's output is a disaster and I had to overcome it. The first version was done quite fast. About 10 minutes and it was done. But I wanted to set underlined style for phpkeywords class which is the color code 007700 in the original output.
This is when it hit me! All the were actually inserted in spans and the underline was taking the entire row. They also considere operators as keywords and inside keywords I also had (){}=- and all other signs.

So I had to turn the disaster above in the beauty below:

As you notice the grey highlights are actually operators which I removed from the kewyord class and created their own operator class. It's all controlled from CSS and the output is an array of lines to use in an OL or UL. The yellow things are the PHP functions which are links to the PHP site documentation.
The functions are searched in a file that has to be named funclist.txt - click to get it. If the file does not exists the function will have regular ordinary look but I will give you the file too so you can make extra pretty PHP formatting to share your PHP scripts online.
The PHP Source Code:
The beautiful code below is output by this very function. It looks so good. And it can be completely altered through CSS as you see in it's header. It can play nicely with HTML code also and link to it if you like it!
<?
function highlight($source,$linkfuncs = false){
$source = trim($source);
$source = highlight_string($source,true);
if (version_compare( phpversion(), "5.0.0", "<")){
$source = str_replace("</font>", "</span>", $source);
$source = preg_replace("/<font color=\"#([0-9A-F]{6})\">/is", "<span style=\"color: #$1\">", $source);
}
$source = preg_replace (
array (
'/.*<code>\s*<span style="color: #000000">/' ,
'/<\/span>\s*<\/code>/',
'/<span[^>]*><\/span>/'
),array('','',''),$source);
$source = str_replace(
array(
'style="color: #0000BB"','style="color: #007700"',
'style="color: #DD0000"','style="color: #FF8000"'
),
array(
'class="phpdefault"','class="phpkeyword"',
'class="phpstring"','class="phpcomment"'
),
$source);
if(preg_match_all("/<span class=\"([^\"]+)\">(.*?)<\/span>/si",$source,$fixSpans)){
for($i=0;$i<count($fixSpans[0]);$i++){
$spanML = $fixSpans[0][$i];
$spanType = $fixSpans[1][$i];
$spanCode = $fixSpans[2][$i];
$spanCode = str_replace("<br />","\n",$spanCode);
$spanCode = preg_replace(
"/&([nsbp;&]+);/i",
"</span>&$1;<span class=\"$spanType\">",
$spanCode
);
$source = str_replace($spanML,
"<span class=\"$spanType\">".nl2br($spanCode)."</span>",
$source
);
}
}
$source = preg_replace("/<span[^>]*><\/span>/i",'',$source);
$source = preg_replace("/<br \/>\s*<br \/>/i",'<br />',$source);
$source = preg_replace("/<br \/>\s+<\/span>/i",'</span><br />',$source);
if(preg_match_all("/<span class=\"phpkeyword\">(.*?)<\/span>/si",$source,$fixKeys)){
$fixKeys = array_unique($fixKeys[1]);
$fixKeys = array_values($fixKeys);
$oldKeys = $fixKeys;
foreach($fixKeys as $fixKeyIndex => $fixKey){
$fixKey = str_replace("<br />","\n",$fixKey);
$fixKeys[$fixKeyIndex]=html_entity_decode($fixKey);
}
for($i=0;$i<count($fixKeys);$i++){
$keyCode = $fixKeys[$i];
$oldCode = $oldKeys[$i];
$keyML = "<span class=\"phpkeyword\">".$oldCode."</span>";
$slices = preg_split("/([a-z_0-9]+)/i",
$keyCode, -1,
PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY
);
$keyNML = "";
foreach($slices as $slice){
if(preg_match("/^[a-z_0-9]+$/i",$slice)){
$keyNML .= "<span class=\"phpkeyword\">".nl2br($slice)."</span>";
continue;
}
$slice = htmlentities($slice);
$keyNML .= "<span class=\"phpoperator\">".nl2br($slice)."</span>";
}
$source = str_replace($keyML, $keyNML, $source);
}
}
$source = preg_replace("/<br \/>\s*<br \/>/i",'<br />',$source);
$source = preg_replace("/<br \/>\s+<\/span>/i",'</span><br />',$source);
$source = preg_replace("/\s+/",' ',$source);
$source = str_replace("\"> ",">",$source);
$funcFile = dirname(__FILE__)."/funclist.txt";
if(file_exists($funcFile)){
$funcsText = file_get_contents($funcFile);
$funcList = preg_split("/[\r\n]+/", $funcsText);
if(preg_match_all("/>([a-z_0-9+]+)</i",$source,$funcs)){
$funcs = $funcs[1];
foreach($funcs as $func){
if(!in_array($func, $funcList)) continue;
if($linkfuncs){
$source = str_replace(">$func<",
"><a href=\"http://php.net/$func\" ".
"rel=\"nofollow\" target=\"_blank\" class=\"phpfunction\">$func</a><",
$source
);
}else{
$source = str_replace(">$func<",
"><span class=\"phpfunction\">$func</span><",
$source
);
}
}
}
}
$source = str_replace("<br />", "\r\n", $source);
if(preg_match_all("/<span class=\"([^\"]+)\">([^<]+)<\/span>/i",$source,$spans)){
for($i=0;$i<count($spans[0]);$i++){
$spanML = $spans[0][$i];
$spanType = $spans[1][$i];
$spanCode = $spans[2][$i];
if(!strstr($spanCode,"\n")) continue;
$spanCode = str_replace("\r\n",
"</span>\r\n<span class=\"$spanType\">", $spanCode
);
$source = str_replace($spanML,
"<span class=\"$spanType\">".$spanCode."</span>", $source
);
}
}
$lines = preg_split("/[\r\n]+/",trim($source));
foreach($lines as $index => $line){
if(strstr($line,"<span")) continue;
$line = "<span class=\"phphtml\">".$line."</span>";
$lines[$index] = $line;
}
return $lines;
}
?>
Post Feedback