note 92449 added to function.highlight-file

From: Date: Fri, 24 Jul 2009 15:42:57 +0000
Subject: note 92449 added to function.highlight-file
Groups: php.notes 
Request: Send a blank email to php-notes+get-158694@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 ============= 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; } } ---- Server IP: 208.69.120.35 Probable Submitter: 173.15.84.89 ---- Manual Page -- http://www.php.net/manual/en/function.highlight-file.php Edit -- https://master.php.net/note/edit/92449 Del: integrated -- https://master.php.net/note/delete/92449/integrated Del: useless -- https://master.php.net/note/delete/92449/useless Del: bad code -- https://master.php.net/note/delete/92449/bad+code Del: spam -- https://master.php.net/note/delete/92449/spam Del: non-english -- https://master.php.net/note/delete/92449/non-english Del: in docs -- https://master.php.net/note/delete/92449/in+docs Del: other reasons-- https://master.php.net/note/delete/92449 Reject -- https://master.php.net/note/reject/92449 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#158694) next »