Aidan's PHP Repository

A repository for PHP functions and classes ...

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

Download this script <?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

December 31st, 2005
i've actually noticed on jexus.dotgeek.org/bb.php that all spaces will be replaced with  , making it quite useless to this regex. my solution was str_replace (' ', ' ', $code), and after function linking: str_replace (' ', '  ', $code);
December 2nd, 2005
Hi Aidan! That function is very usefully. But i've changed some lines of your function to get a better result. This fixed a copy-problem of the output and an format error of line numbering "1". Copy problem: If users will copy the php code, they must remove every line numbering before they can test or use the code sample. Format error: The line numbering "1" is not formatted by the code-tag. That looks a little bit... Changes: \n"; # Strip the code-tag to fix the format error. $text .= str_replace(array("\n", '', ''), '', $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 . '
'; // Return mode #... } ?> That's all! Many greetings Daniel Kressler
October 15th, 2005
you could replace the replacement bit for functions with this for php4 compatibility: )'. // Followed by a bracket '(\s*\s*\()~m', // Replace with a link to the manual '$1$2$3', $text); } else { $text = preg_replace( // Match a highlighted keyword '~([\w_]+)(\s*)'. // Followed by a bracket '(\s*\s*\()~m', // Replace with a link to the manual '$1$2$3', $text); } ?> i'm using the regex in my current project for function highlighting, and there i've built in a function which check if it's defined (function_exists) AND checks if it doesn't show up in the array of user defined functions. i hope this solves problems with functions highlighted that don't exist! [Editor's Note: Looks great jexus, I've added this in.]