PHP color coder (like on phpdn and php.net

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

PHP color coder (like on phpdn and php.net

Post by Jonah Bron »

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.

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;
?>
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Post by Jonah Bron »

Ha ha. Like my dad says: K.I.S (keep it simple, with out the stupid :wink: ). Didn't know about that function. Thanks :D



B.T.W. Congrats on your mod status
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Thank you! :)
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
sunilbhatia79
Forum Newbie
Posts: 24
Joined: Sun Nov 11, 2007 9:37 pm
Location: Mumbai, India

Post by sunilbhatia79 »

You can also use this library:
http://qbnz.com/highlighter/
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

GeSHi, the library mentioned in the post above, is a fairly heavy library. It offers a lot of cool features, but if all you are looking for is syntax highlighting of PHP code, use the built-ins scottayy mentioned.

Just make sure when using those functions that the code you are highlighting opens with <?php and not <? or it may not highlight.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Post by Jonah Bron »

Okay, thanks, everyone!
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Post by Jonah Bron »

yes, color coding syntax is all I was looking for. Thanks, everyone!
Post Reply