demo of highlight_file with CSS classes, line numbers, and optionally   replaced

1. syntax highlighted display:
001<?php
002/* highlight_file with CSS classes and line numbers
003 NOT THOROUGHLY TESTED - USE AT YOUR OWN RISK
004 Author: Andy Wrigley (http://means.us.com) */

005ini_set('highlight.default', '"class="aw_default'); ini_set('highlight.keyword','"class="aw_keyword');
006ini_set('highlight.string', '"class="aw_string'); ini_set('highlight.html', '"class="aw_htmlsrc');
007ini_set('highlight.comment', '"class="aw_comment');
008$aw_source = highlight_file('YOUR_FILE.HERE', TRUE);
009$aw_source = str_replace('<code>', '' , $aw_source );
010$aw_source = str_replace(array("\r\n","\r","\n"),'',$aw_source);
011$aw_source = trim($aw_source);
012$aw_source = str_replace('style="color: "', '' , $aw_source );
013# $aw_source = str_replace("&nbsp;", ' ', $aw_source);
014$aw_source=str_replace('<br /></span>','</span><br />',$aw_source);
015$aw_lines = explode("<br />", $aw_source);
016echo
"<code>";
017$i = 1;
018foreach (
$aw_lines as $aw_line){
019
$line_disp = '<span class="linenum">' . sprintf("%03d",$i) .'</span>';
020
$line_disp .= $aw_line;
021 echo
$line_disp . "<br />";
022
$the_html .= '<span style="color:red">&lt;!-- line ' . $i . ' --&gt;</span>' . htmlentities($line_disp) . '&lt;br /&gt;<br />';
023$i++; }
024echo
"<br /><h3>source of listing for copy and paste</h3><pre>&lt;code&gt;<br />" . $the_html . '</pre>';
025/* if you don't want to display the HTML source for the listing
026then delete lines 22 and 24 */

027?>

2. source of listing, for copy and paste

<code>
<!-- line 1 --><span class="linenum">001</span><span class="aw_htmlsrc"><span class="aw_default">&lt;?php</span><br />
<!-- line 2 --><span class="linenum">002</span><span class="aw_comment">/* highlight_file with CSS classes and line numbers<br />
<!-- line 3 --><span class="linenum">003</span> NOT THOROUGHLY TESTED - USE AT YOUR OWN RISK<br />
<!-- line 4 --><span class="linenum">004</span> Author: Andy Wrigley (http://means.us.com) */</span><br />
<!-- line 5 --><span class="linenum">005</span><span class="aw_default">ini_set</span><span class="aw_keyword">(</span><span class="aw_string">'highlight.default'</span><span class="aw_keyword">, </span><span class="aw_string">'"class="aw_default'</span><span class="aw_keyword">); </span><span class="aw_default">ini_set</span><span class="aw_keyword">(</span><span class="aw_string">'highlight.keyword'</span><span class="aw_keyword">,</span><span class="aw_string">'"class="aw_keyword'</span><span class="aw_keyword">); </span><br />
<!-- line 6 --><span class="linenum">006</span><span class="aw_default">ini_set</span><span class="aw_keyword">(</span><span class="aw_string">'highlight.string'</span><span class="aw_keyword">, </span><span class="aw_string">'"class="aw_string'</span><span class="aw_keyword">); </span><span class="aw_default">ini_set</span><span class="aw_keyword">(</span><span class="aw_string">'highlight.html'</span><span class="aw_keyword">, </span><span class="aw_string">'"class="aw_htmlsrc'</span><span class="aw_keyword">);</span><br />
<!-- line 7 --><span class="linenum">007</span><span class="aw_default">ini_set</span><span class="aw_keyword">(</span><span class="aw_string">'highlight.comment'</span><span class="aw_keyword">, </span><span class="aw_string">'"class="aw_comment'</span><span class="aw_keyword">);</span><br />
<!-- line 8 --><span class="linenum">008</span><span class="aw_default">$aw_source </span><span class="aw_keyword">= </span><span class="aw_default">highlight_file</span><span class="aw_keyword">(</span><span class="aw_string">'YOUR_FILE.HERE'</span><span class="aw_keyword">, </span><span class="aw_default">TRUE</span><span class="aw_keyword">);</span><br />
<!-- line 9 --><span class="linenum">009</span><span class="aw_default">$aw_source </span><span class="aw_keyword">= </span><span class="aw_default">str_replace</span><span class="aw_keyword">(</span><span class="aw_string">'&lt;code&gt;'</span><span class="aw_keyword">, </span><span class="aw_string">'' </span><span class="aw_keyword">, </span><span class="aw_default">$aw_source </span><span class="aw_keyword">);</span><br />
<!-- line 10 --><span class="linenum">010</span><span class="aw_default">$aw_source </span><span class="aw_keyword">= </span><span class="aw_default">str_replace</span><span class="aw_keyword">(array(</span><span class="aw_string">"\r\n"</span><span class="aw_keyword">,</span><span class="aw_string">"\r"</span><span class="aw_keyword">,</span><span class="aw_string">"\n"</span><span class="aw_keyword">),</span><span class="aw_string">''</span><span class="aw_keyword">,</span><span class="aw_default">$aw_source</span><span class="aw_keyword">);</span><br />
<!-- line 11 --><span class="linenum">011</span><span class="aw_default">$aw_source </span><span class="aw_keyword">= </span><span class="aw_default">trim</span><span class="aw_keyword">(</span><span class="aw_default">$aw_source</span><span class="aw_keyword">);</span><br />
<!-- line 12 --><span class="linenum">012</span><span class="aw_default">$aw_source </span><span class="aw_keyword">= </span><span class="aw_default">str_replace</span><span class="aw_keyword">(</span><span class="aw_string">'style="color: "'</span><span class="aw_keyword">, </span><span class="aw_string">'' </span><span class="aw_keyword">, </span><span class="aw_default">$aw_source </span><span class="aw_keyword">);</span><br />
<!-- line 13 --><span class="linenum">013</span><span class="aw_comment"># $aw_source = str_replace("&amp;nbsp;", ' ', $aw_source);</span><br />
<!-- line 14 --><span class="linenum">014</span><span class="aw_default">$aw_source</span><span class="aw_keyword">=</span><span class="aw_default">str_replace</span><span class="aw_keyword">(</span><span class="aw_string">'&lt;br /&gt;&lt;/span&gt;'</span><span class="aw_keyword">,</span><span class="aw_string">'&lt;/span&gt;&lt;br /&gt;'</span><span class="aw_keyword">,</span><span class="aw_default">$aw_source</span><span class="aw_keyword">);</span><br />
<!-- line 15 --><span class="linenum">015</span><span class="aw_default">$aw_lines </span><span class="aw_keyword">= </span><span class="aw_default">explode</span><span class="aw_keyword">(</span><span class="aw_string">"&lt;br /&gt;"</span><span class="aw_keyword">, </span><span class="aw_default">$aw_source</span><span class="aw_keyword">);<br />
<!-- line 16 --><span class="linenum">016</span>echo </span><span class="aw_string">"&lt;code&gt;"</span><span class="aw_keyword">;</span><br />
<!-- line 17 --><span class="linenum">017</span><span class="aw_default">$i </span><span class="aw_keyword">= </span><span class="aw_default">1</span><span class="aw_keyword">;<br />
<!-- line 18 --><span class="linenum">018</span>foreach (</span><span class="aw_default">$aw_lines </span><span class="aw_keyword">as </span><span class="aw_default">$aw_line</span><span class="aw_keyword">){<br />
<!-- line 19 --><span class="linenum">019</span> </span><span class="aw_default">$line_disp </span><span class="aw_keyword">= </span><span class="aw_string">'&lt;span class="linenum"&gt;' </span><span class="aw_keyword">. </span><span class="aw_default">sprintf</span><span class="aw_keyword">(</span><span class="aw_string">"%03d"</span><span class="aw_keyword">,</span><span class="aw_default">$i</span><span class="aw_keyword">) .</span><span class="aw_string">'&lt;/span&gt;'</span><span class="aw_keyword">;<br />
<!-- line 20 --><span class="linenum">020</span> </span><span class="aw_default">$line_disp </span><span class="aw_keyword">.= </span><span class="aw_default">$aw_line</span><span class="aw_keyword">;<br />
<!-- line 21 --><span class="linenum">021</span> echo </span><span class="aw_default">$line_disp </span><span class="aw_keyword">. </span><span class="aw_string">"&lt;br /&gt;"</span><span class="aw_keyword">;<br />
<!-- line 22 --><span class="linenum">022</span> </span><span class="aw_default">$the_html </span><span class="aw_keyword">.= </span><span class="aw_string">'&lt;span style="color:red"&gt;&amp;lt;!-- line ' </span><span class="aw_keyword">. </span><span class="aw_default">$i </span><span class="aw_keyword">. </span><span class="aw_string">' --&amp;gt;&lt;/span&gt;' </span><span class="aw_keyword">. </span><span class="aw_default">htmlentities</span><span class="aw_keyword">(</span><span class="aw_default">$line_disp</span><span class="aw_keyword">) . </span><span class="aw_string">'&amp;lt;br /&amp;gt;&lt;br /&gt;'</span><span class="aw_keyword">;</span><br />
<!-- line 23 --><span class="linenum">023</span><span class="aw_default">$i</span><span class="aw_keyword">++; } <br />
<!-- line 24 --><span class="linenum">024</span>echo </span><span class="aw_string">"&lt;br /&gt;&lt;h3&gt;source of listing for copy and paste&lt;/h3&gt;&lt;pre&gt;&amp;lt;code&amp;gt;&lt;br /&gt;" </span><span class="aw_keyword">. </span><span class="aw_default">$the_html </span><span class="aw_keyword">. </span><span class="aw_string">'&lt;/pre&gt;'</span><span class="aw_keyword">;</span><br />
<!-- line 25 --><span class="linenum">025</span><span class="aw_comment">/* if you don't want to display the HTML source for the listing<br />
<!-- line 26 --><span class="linenum">026</span>then delete lines 22 and 24 */</span><br />
<!-- line 27 --><span class="linenum">027</span><span class="aw_default">?&gt;</span></span></code><br />

Brought to you by means.us.com