highlight_file

Descrierea

Prints out or returns a syntax highlighted version of the code contained
in filename using the colors defined in the
built-in syntax highlighter for PHP.

Many servers are configured to automatically highlight files
with a phps extension. For example,
example.phps when viewed will show the
syntax highlighted source of the file. To enable this, add this
line to the httpd.conf:

AddType application/x-httpd-php-source .phps

Parametri

filename

Path to the PHP file to be highlighted.

return

Set this parameter to TRUE to make this function return the
highlighted code.

Valorile întoarse

If return is set to TRUE, returns the highlighted
code as a string instead of printing it out. Otherwise, it will return
TRUE on success, FALSE on failure.

Istoricul schimbărilor

Note

Precauţie

Care should be taken when using the highlight_file()
function to make sure that you do not inadvertently reveal sensitive
information such as passwords or any other type of information that might
create a potential security risk.

This works because when highlight_file() writes out a comment it wraps it in <span style="color: {contents of hightlight.comment ini variable}">...</span>. Therefore as long as your ini value for highlight.comment starts with a color, you can put in whatever style declarations you like.

This applies to all of the other relavent ini values: highlight.default, highlight.html, highlight.keyword, highlight.string.

Please, be aware of just using marlon at mbwp dot nl example to show the sourcecodeDo as told in this manual, "CAUTION", do some check on what code to highlight.Simply just using $_GET["file"] is very bad.

Here is a simple, yet useful, tip... Issuing the following command under a Unix-like OS:

ln -s your_script.php your_script.phps

Will result in the creation of a symbolic link (called your_script.phps) to the source code (your_script.php). This way you have (apart from the source code .php) the highlighted version of your script (.phps) accessible via your web browser.

// get the filename$file = $_GET[file];if(file_Exists($file)){ if(in_array($file, $allowed)){// check if it is part of the allowed listhighlight_file($file); //highlight file} else{// not allowed. just die. do not warn ;)die(""); }}else{// file doesnt existecho "The file does not exist."; }?>

A function which parses the output of highlight file and replaces &lt;span&gt; wits &lt;a&gt;, replaces the style="color: #XXXXXX" part with class="cX", storing the colors in some css rules; that way you get a smaller output size (ratio is about about 1.2 ~ 1.4):<code><?phpfunction highlight_file2($fl,$ret){ if(!isset($ret)) $ret = false;$str = highlight_file($fl,true);preg_match_all("/\<span style=\"color: #([\d|A|B|C|D|E|F]{6})\"\>.*?\<\/span\>/",$str,$mtch);$m = array_unique($mtch[1]);

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).

From W on 20 August 2008:YES! I do it too, on many of my things! I love to be able to share my code (when I have the right to do it) with friends who aren't working on the project.

<?php

if (isset($_GET['code'])) { die(highlight_file(__FILE__, 1)); }

?>

I like to do it like this. The benefit is minimal but personally for such a small thing I would like to keep it down to one line. Maybe there is a shorter way, I don't know.. But the magic constant __FILE__ saves the day!

To access this one would be http://example.com/path/to/script.php?codethe parameter just has to be set (? or &) and you don't need the equals sign.This way I don't generate any errors in E_ALL (because I'm likely in development), and I don't have to remember if the value was supposed to be "true" or "1" or "abacus" or whatever (I mean you're never going to specifically ask to NOT show the code, right?)

Anyway I've gone on long enough. It's nice to not be the only one who does this :D

I use this (simple and dirty) function in my tutorials to add linenumbers. I let all the linenumbers in one table cell so it is easier for people to copy and paste the code (so they don't have to remove all the linenumbers):

// Supposing we open more spans on a line than closing... let's do this properly
// We have at most one currently opened span
if( substr_count($line, '<span') - substr_count($line, '</span') > 0 )
{
$line .= '</span>';

// Grab colour to report it on next line
$last_colour = explode('<span ',$line);
$last_colour = $last_colour[count($last_colour)-1];