note 92449 modified in function.highlight-file by danbrown

From: Date: Fri, 24 Jul 2009 15:50:38 +0000
Subject: note 92449 modified in function.highlight-file by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-158696@lists.php.net to get a copy of this message
This function will not only echo out the code but will add line numbers. The extra code is for the multi-line and single-line comments using /* and */ and making sure they are highlighted appropriately. If not used, the multi-line comments will not be displayed in orange (or the default comment color). Anthony ============= <?php function renderFile($filename) { if(file_exists($filename) && is_file($filename)) { $code = highlight_file($filename, true); $counter = 1; $arr = explode('<br />', $code); echo '<table border="0" cellpadding="0" cellspacing="0" width="100%" style="font-family: monospace;">' . "\r\n"; foreach($arr as $line) { echo '<tr>' . "\r\n"; echo '<td width="65px" nowrap style="color: #666;">' . $counter . ':</td>' . "\r\n"; // fix multi-line comment bug if((strstr($line, '<span style="color: #FF8000">/*') !== false) && (strstr($line, '*/') !== false)) { // single line comment using /* */ $comments = false; $startcolor = "orange"; } elseif(strstr($line, '<span style="color: #FF8000">/*') !== false) { // multi line comment using /* */ $startcolor = "orange"; $comments = true; } else { // no comment marks found $startcolor = "green"; if($comments) { // continuation of multi line comment if(strstr($line, '*/') !== false) { $comments = false; $startcolor = "orange"; } else { $comments = true; } } else { // normal line $comments = false; $startcolor = "green"; } } // end fix multi-line comment bug if($comments) echo '<td width="100%" nowrap style="color: orange;">' . $line . '</td>' . "\r\n"; else echo '<td width="100%" nowrap style="color: ' . $startcolor . ';">' . $line . '</td>' . "\r\n"; echo '</tr>' . "\r\n"; $counter++; } echo '</table>' . "\r\n"; } else { echo "<p>The file <i>$filename</i> could not be opened.</p>\r\n"; return; } } ?> --was-- This function will not only echo out the code but will add line numbers. The extra code is for the multi-line and single-line comments using /* and */ and making sure they are highlighted appropriately. If not used, the multi-line comments will not be displayed in orange (or the default comment color). Anthony ============= function renderFile($filename) { if(file_exists($filename) && is_file($filename)) { $code = highlight_file($filename, true); $counter = 1; $arr = explode('<br />', $code); echo '<table border="0" cellpadding="0" cellspacing="0" width="100%" style="font-family: monospace;">' . "\r\n"; foreach($arr as $line) { echo '<tr>' . "\r\n"; echo '<td width="65px" nowrap style="color: #666;">' . $counter . ':</td>' . "\r\n"; // fix multi-line comment bug if((strstr($line, '<span style="color: #FF8000">/*') !== false) && (strstr($line, '*/') !== false)) { // single line comment using /* */ $comments = false; $startcolor = "orange"; } elseif(strstr($line, '<span style="color: #FF8000">/*') !== false) { // multi line comment using /* */ $startcolor = "orange"; $comments = true; } else { // no comment marks found $startcolor = "green"; if($comments) { // continuation of multi line comment if(strstr($line, '*/') !== false) { $comments = false; $startcolor = "orange"; } else { $comments = true; } } else { // normal line $comments = false; $startcolor = "green"; } } // end fix multi-line comment bug if($comments) echo '<td width="100%" nowrap style="color: orange;">' . $line . '</td>' . "\r\n"; else echo '<td width="100%" nowrap style="color: ' . $startcolor . ';">' . $line . '</td>' . "\r\n"; echo '</tr>' . "\r\n"; $counter++; } echo '</table>' . "\r\n"; } else { echo "<p>The file <i>$filename</i> could not be opened.</p>\r\n"; return; } } http://php.net/manual/en/function.highlight-file.php

« previous php.notes (#158696) next »