PHP color coder (like on phpdn and php.net
Posted: Thu Nov 15, 2007 10:19 am
Hi,
I am working on a php color coder for my blog. Like on phpdn and php.net. This is what I have so far, but if the word "if" or "echo" or anything like that comes up inside quotes, it is still colored. Same with comments (forward slashes (//)).
The only problem left, is how to get it to stop applying the coloration to words inside quotes.
I am working on a php color coder for my blog. Like on phpdn and php.net. This is what I have so far, but if the word "if" or "echo" or anything like that comes up inside quotes, it is still colored. Same with comments (forward slashes (//)).
The only problem left, is how to get it to stop applying the coloration to words inside quotes.
Code: Select all
<?php
//apply specified color function
function color($col,$con){
return '<span style="color:#'.$col.';">'.$con.'</span>';
}
//php code
$php = "<?php
if ($x=='qwerty'){
echo 'billy bob';
}
//comment line
echo 'string //comment';
print 'bob';
?>";
$php = explode('
',$php);
$count = count($php);
$x = 0;
while ($x<$count){
$php[$x] = str_replace('//','<span style="color:#888;">//',$php[$x]);
if (eregi('<span style="color:#888;">//',$php[$x])){
$php[$x] .= '</span>';
}else{
$php[$x] = str_replace('<?php',color('0a0','<?php').'<span style="color:00f;">',$php[$x]);
$php[$x] = str_replace('?>','</span>'.color('0a0','?>'),$php[$x]);
$php[$x] = str_replace('if',color('0f0','if'),$php[$x]);
$php[$x] = str_replace('else','<span style="color:#0f0;">else</span>',$php[$x]);
$php[$x] = str_replace('elseif','<span style="color:#0f0;">elseif</span>',$php[$x]);
$php[$x] = str_replace('echo',color('0f0','echo'),$php[$x]);
$php[$x] = str_replace('return',color('0f0','return'),$php[$x]);
$php[$x] = str_replace('print',color('0f0','print'),$php[$x]);
}
$x += 1;
}
$php = implode('<br />
',$php);
echo $php;
?>