Click to See Complete Forum and Search --> : PHP driven Syntax Highlighter
I_Love_My_Vans
Nov 8th, 2006, 08:13 AM
I have tried using some syntax highlights before, and they all suck :D
So problem solved, i will write my own PHP function to highlight the syntax for me :D
Now i would like some guidance on the general workings of this, as i cant quite figure it all out.
Im going to need to loop through the characters of the string (the code i want to highlight), whilst picking out certain keywords, copmment lines etc to highlight.
Anyone help? Cheersio
kows
Nov 8th, 2006, 10:01 AM
if you're highlighting PHP syntax (it would have probably been better if you said something about the language you were using), then PHP has its own functions built in that will do it for you.
I made a PHP source thing a while ago, so for this, you just need to basically look into the function highlight_string(). Here is an example of how you could use it:
<?php
$file = file_get_contents('index.php');
highlight_string($file);
?>
If you want to manipulate it (add line numbers, etc), use ob_start() before calling the function, then highlight your $file, and then ob_get_contents() to trap the HTML it outputs. Then you can just clear the buffer (ob_end_clean()) so that you can get rid of everything you saved in it. This way, you can change the contents and do your own few things without it outputting until you want it to. This is a full fledged example of that:
<style>
div {
font-size: 10pt;
}
#source {
border: 1px solid #000;
display: table;
width: 100%;
}
.line {
clear: both;
width: 100%;
display: block;
}
.lineno {
font-family: arial;
width: 25px;
float: left;
padding-left: 5px;
background-color: #efefef;
border-right: 1px solid #c0c0c0;
}
.text {
font-family: courier new;
float: left;
margin-left: 10px;
}
</style>
<?php
$file = file_get_contents('index.php');
ob_start();
highlight_string($file);
$syntax = ob_get_contents();
ob_end_clean();
//let's add line numbers
$lines = explode("\r<br />", $syntax);
?>
<div id="source">
<?php
for($i = 0; $i < count($lines); $i++){
echo ' <div class="line">' . "\r\n";
echo ' <div class="lineno"><font color=#c0c0c0>' . ($i + 1) . '</font></div><div class="text"><nobr>' . $lines[$i] . '</nobr></div>' . "\r\n";
echo ' </div>' . "\r\n";
}
?>
</div>
I just made it (only because the old script I had was somewhat badly written, and it might need a bit of tweaking on the style sheet's part for lines of text that will scroll off of the edge if you don't want them to break at all (although, the line structure will stay intact, the line will just break). I only tested it with the index.php of my local webserver, but the syntax stuff all works alright.
if you aren't highlighting PHP syntax, then you will probably want to learn about regular expressions if you haven't already, as you'll be doing a lot of preg_replace()'s. Looping through characters is probably unneeded, and would take an awfully long time as well. Looping through lines, depending on the structure of the language you've chosen, might be needed.
penagate
Nov 9th, 2006, 12:01 AM
You don't need to use output buffering, just tell highlight_string to return instead of outputting.
$coloured_code = highlight_string($file, true);
kows
Nov 9th, 2006, 12:14 AM
oops, didn't know they added the second parameter. last time I used it was before php 4.2. thanks for the heads up, though.
I_Love_My_Vans
Nov 9th, 2006, 02:37 PM
OH, its for VB code :P
penagate
Nov 9th, 2006, 09:14 PM
Use geshi (http://qbnz.com/highlighter/) or preg_replace.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.