Blogs with code samples should make those samples easy to read and, if not easy to understand, at least easy to copy. And I always appreciate code samples when they’re highlighted like my favorite text editor. Finding an existing utility to transform PHP into web-ready html took longer than it took me to write my own (which isn’t completely effective, but covers the majority of my style). If you’re looking for similar results and want a tool that’s easily editable, here you go:
#!/usr/bin/perl $code_file = 'Paydate_Calculator.php' or die "Cannot open filen"; open (CODE, $code_file); @code_lines = <CODE>; close(CODE); $html_file = '>'.$code_file.'.html'; open (HTML, $html_file); print HTML "<pre>n"; print HTML <<HERE; <style> pre { font: 12px "Courier New"; } .php_tag { color: red; } .php_comment { color: green; } .php_reserved { font-weight: bold; color: #000FF1; } .php_variable { color: navy; } .php_int { color: orange; font-weight: bold; } .php_string { color: #555555; } </style> HERE $comment = 0; foreach $line (@code_lines) { $line =~ s/</</; #OPEN < $line =~ s/>/>/; #CLOSE > if (($comment == 0)&&($line =~ m//*/)&&($line !~ m/*// )) { $line =~ s//*/<span class='php_comment'>$&$'/; #OPEN C-STYLE $comment = 1; } if (($comment == 1)&&($line =~ m/*//)&&($line !~ m//*/)) { #CLOSE C-STYLE $line =~ s/(.*)(*/)(.*)/$1$2</span>$3/; $comment = 0; } if ($comment == 0) { #skipping transform on c-style comments #RESERVED WORDS if ($line =~ ////) { #Line matches an inline comment @parts = split('//', $line); $non_comment = $parts[0]; #Not commented $non_comment = &apply_styles($non_comment); shift(@parts); #Now the array is only the commented parts $commented = "@parts"; $commented = "<span class='php_comment'>//$commented</span>"; $line = $non_comment.$commented; } else { $line = &apply_styles($line); } #Finally change the Reserved word "class" $line =~ s/[s](class)[s]/<span class='php_reserved'>$&</span>/g; } print HTML $line; } print HTML "</pre>"; close(HTML); sub apply_styles() { $some_string = $_[0]; $php_sei = '~PHP_STRING_EDITOR_INDEX~'; %php_strings = (); #empty hash $count = 0; while ($some_string =~ /('([^']*)')/) { #it matches any single-quote comment #temporarily replace the string with a slug that we won't set classes on $some_string =~ s/('([^']*)')/${php_sei}PHPSI:${count}${php_sei}/; $php_strings{"PHPSI:".$count} = $1; $count ++; } $some_string =~ s/[^(PHPSI:)]d+/<span class='php_int'>$&</span>/g; #Ints $some_string =~ s/<?php|?>/<span class='php_tag'>$&</span>/; #PHP TAG $some_string =~ s/($w+)b/<span class='php_variable'>$1</span>/g; #Variables $match = '( if|function|echo|print|is_null|isset|private| public|static|switch|case|break|return|true|false|null)'; $some_string =~ s/$match/<span class='php_reserved'>$1</span>/xg; #Put strings back in $s_index_match = $php_sei.'(PHPSI:d+)'.$php_sei; $some_string =~ s/$s_index_match/<span class='php_string'>$php_strings{$1}</span>/g; return $some_string; }
Make backups of your work, of course. I don’t want to be responsible for a transform that turns 40 hours of effort into: ”. In addition, my love for perl is due to its ability to take me, a horrible coder, and turn me into something useful. I’m not writing code for tiny embedded chips, so please excuse the bloated, c-style approach.

