highlight_file with CSS classes, line numbers, and   replaced

this page lists itself

001<style type="text/css">.aw_default {color:blue} .aw_keyword{color:green}
002 .aw_comment{color:brown;font-weight:bold} .aw_string{color:red} .aw_htmlsrc{color:black;}
003 .linenum{font-weight:normal !important; border-right:1px solid grey;color:black;text-align:right;padding:0 0.2em;margin-right:0.5em;}
004</style>
005<h3>highlight_file with CSS classes, line numbers, and &amp;nbsp; replaced</h3>
006<h3>this page lists itself</h3>
007<pre>
008<?php
009/* tricks highlight_file into using CSS classes and
010 adds line numbers to output
011 NOT THOROUGHLY TESTED - USE AT YOUR OWN RISK
012 Author: Andy Wrigley (http://means.us.com)
013*/
014/* for explanation of injecting classes see
015http://wptest.means.us.com/2012/06/php-tricking-highlight_file-to-use-css-classes-instead-of-inline-styles/ */

016ini_set('highlight.default', '"class="aw_default'); ini_set('highlight.keyword','"class="aw_keyword');
017ini_set('highlight.string', '"class="aw_string'); ini_set('highlight.html', '"class="aw_htmlsrc');
018ini_set('highlight.comment', '"class="aw_comment');
019# Run the PHP syntax highlighter on our file and return as string
020$aw_source = highlight_file('list_code_linenum_classes_nonbsp.php', TRUE);
021# remove stuff that affects our formating
022$aw_source = str_replace('<code>', '' , $aw_source );
023$aw_source = str_replace(array("\r\n","\r","\n"),'',$aw_source);
024$aw_source = trim($aw_source);
025# Remove empty inline styles
026$aw_source = str_replace('style="color: "', '' , $aw_source );
027# you should DELETE NEXT LINE if returned string is NOT used within pre tag
028$aw_source = str_replace("&nbsp;", ' ', $aw_source);
029# use <br /> as the separator to explode $aw_source into an array
030$aw_source = str_replace('<br /></span>', '</span><br />' , $aw_source );
031$aw_lines = explode("<br />", $aw_source);
032# print our source listing
033echo "<code>";
034$i = 1;
035foreach (
$aw_lines as $aw_line){
036
#insert line num as 3 digits with leading 0s
037
$line_disp = '<span class="linenum">' . sprintf("%03d",$i) .'</span>';
038
$line_disp .= $aw_line;
039 echo
$line_disp . "<br />";
040# $the_html .= '<span style="color:red">&lt;!-- line ' . $i . ' --&gt;</span>' . htmlentities($line_disp) . '&lt;br /&gt;<br />';
041$i++; }
042# echo "<br /><br /><h3>for copy and paste</h3><pre>&lt;code&gt;<br />" . $the_html . '</pre>';
043/* UNCOMMENT LINEs 40 and 42 if you also wish to display HTML
044for the listing. Use it to copy complete listing or snippets
045into other pages or "post editors" like Wordpress. */

046?></pre>
047<h2>Brought to you by <a href="means.us.com">means.us.com</a></h2>

Brought to you by means.us.com