Index: trac/mimeview/php.py
===================================================================
--- trac/mimeview/php.py	(revision 3486)
+++ trac/mimeview/php.py	(working copy)
@@ -21,6 +21,7 @@
 from trac.mimeview.api import IHTMLPreviewRenderer, content_to_unicode
 from trac.util import NaivePopen
 from trac.util.markup import Deuglifier
+import re
 
 __all__ = ['PHPRenderer']
 
@@ -30,6 +31,19 @@
 
 class PhpDeuglifier(Deuglifier):
 
+    def format(self, indata):
+        # The PHP highlighter produces the end-span tags on the next line
+        # instead of the line they actually apply to, which causes
+        # Trac to produce lots of (useless) open-and-immediately-close
+        # spans beginning each line.  This tries to curtail by bubbling
+        # the first span after a set of 1+ "<br />" to before them.
+        r_fixeol = re.compile(r"((?:<br />)+)(</(?:font|span)>)")
+        indata = r_fixeol.sub(lambda m: m.group(2) + m.group(1), indata)
+        
+        # Now call superclass implementation that handles the dirty work
+        # of applying css classes.
+        return Deuglifier.format(self, indata)
+
     def rules(cls):
         colors = dict(comment='FF8000', lang='0000BB', keyword='007700',
                       string='DD0000')
@@ -71,7 +85,7 @@
             err = 'Running (%s) failed: %s, %s.' % (cmdline, np.errorlevel,
                                                     np.err)
             raise Exception, err
-        odata = ''.join(np.out.splitlines()[1:-1])
+        odata = ''.join(np.out.splitlines()[1:-2])
         if odata.startswith('X-Powered-By'):
             raise TracError, 'You appear to be using the PHP CGI binary.  ' \
                              'Trac requires the CLI version for syntax ' \

