// this function is used to find the last occurance of a string within another string // it's used because strrpos() only looks for a single character, not a string function strLastPos($haystack, $needle) { // flip both strings around and search, then adjust position based on string lengths return strlen($haystack) - strlen($needle) - strpos(strrev($haystack), strrev($needle)); } // transform my little code tags function codeTransform($filename) { $filename = 'code/'.$filename.'.txt'; // set vars $list = ''; $cmnt = ''; $multi_line_cmnt = 0; $tab = ''; $class = ''; // open the file $file = fopen($filename, 'r'); // for each line in the file while (!feof($file)) { // get line $line = fgets($file, 4096); // convert tags to safe entities for display $line = htmlspecialchars($line); // count the number of tabs at the start of the line, and set the appropriate tab class $tab = substr_count($line, "\t"); $tab = ($tab > 0) ? 'tab'.$tab : ''; // remove any tabs at the start of the line $line = str_replace("\t", '', $line); // find position of comment characters $slashslash_pos = strpos($line, '//'); $apos_pos = strpos($line, "'"); $slashstar_pos = strpos($line, '/*'); $starslash_pos = strLastPos($line, '*/'); // if it's an ongoing multi-line comment if ($multi_line_cmnt == 1) { $cmnt = 'cmnt'; $multi_line_cmnt = 1; } // if it's not an ongoing multi-line comment if ($multi_line_cmnt <> 1) { // if it's a single line comment if (($slashslash_pos === 0) || ($apos_pos === 0)) { $cmnt = 'cmnt'; $multi_line_cmnt = 0; } else { $cmnt = ''; $multi_line_cmnt = 0; } // if it's potentially the start of a multi-line comment if ($slashstar_pos === 0) { $cmnt = 'cmnt'; // if multi-line comment end string is found on the same line if ($starslash_pos == (strlen($line) - 3)) { $multi_line_cmnt = 0; } // if the multi-line comment end string is not found on the same line else { $multi_line_cmnt = 1; } } } // if the line contains the multi-line end string if ($starslash_pos == (strlen($line) - 3)) { $cmnt = 'cmnt'; $multi_line_cmnt = 0; } // if both cmnt and tab classes are to be applied if ( ($cmnt <> '') && ($tab <> '') ) { $class = ' class="'.$tab.' '.$cmnt.'"'; } // if only one class is to be applied else if ( ($cmnt <> "") || ($tab <> "") ) { $class = ' class="'.$tab.$cmnt.'"'; } // if no classes are to be applied else { $class = ''; } // remove return at the end of the line $line = str_replace("\n", "", $line); // if the line is blank, put a space in to stop some browsers collapsing the line if ($line == '') { // insert all the information and close the list item $list .= '
'.$line.'