function.highlight_filei.php
Highlight a file.
Provides better syntax highlighting, line numbering and function linking.
- Author: Aidan Lister <aidan@php.net>
- Version: 1.1.0
- Link: http://aidanlister.com/repos/v/function.highlight_fiei.php
- Views: 6283
- Downloads: 1508
Source
<?php
/**
* Highlight a file.
*
* Provides better syntax highlighting, line numbering and function linking.
*
* @author Aidan Lister <aidan@php.net>
* @version 1.1.0
* @link http://aidanlister.com/repos/v/function.highlight_fiei.php
* @param string $data The string to add line numbers to
* @param bool $return return or echo the data
* @param bool $linenum Add line numbers or not
* @param bool $funclink Automatically link functions to the manual
*/
function highlight_filei($file, $return = false, $linenum = true, $funclink = true)
{
// Init
$data = explode('<br />', file_get_contents($file));
$start = '<span style="color: black;">';
$end = '</span>';
$i = 1;
$text = '';
// Loop
foreach ($data as $line) {
$text .= $start . $i . ' ' . $end .
str_replace("\n", '', $line) . "<br />\n";
++$i;
}
// Optional function linking
if ($funclink === true) {
$keyword_col = ini_get('highlight.keyword');
$manual = 'http://www.php.net/function.';
if (version_compare('5.0.0', PHP_VERSION) === -1) {
$text = preg_replace(
// Match a highlighted keyword
'~([\w_]+)(\s*</span>)'.
// Followed by a bracket
'(\s*<span\s+style="color: ' . $keyword_col . '">\s*\()~m',
// Replace with a link to the manual
'<a href="' . $manual . '$1">$1</a>$2$3', $text);
} else {
$text = preg_replace(
// Match a highlighted keyword
'~([\w_]+)(\s*</font>)'.
// Followed by a bracket
'(\s*<font\s+color="' . $keyword_col . '">\s*\()~m',
// Replace with a link to the manual
'<a href="' . $manual . '$1">$1</a>$2$3', $text);
}
}
// Return mode
if ($return === false) {
echo $text;
} else {
return $text;
}
}
?>
Comments
', ''), '', $line) . "\n"; ++$i; } // Optional function linking #... # Add code-tags to get a correct format. # Add table cells at $lines and $text for easier copying. $text = '
' . $lines . '' . $text . '