highlight_file

(PHP4 )

highlight_file -- Syntax highlighting of a file

Description

void highlight_file (string filename)

The highlight_file() function prints out a syntax higlighted version of the code contained in filename using the colors defined in the built-in syntax highlighter for PHP.

Példa 1. Creating a source highlighting URL

To setup a URL that can code hightlight any script that you pass to it, we will make use of the "ForceType" directive in Apache to generate a nice URL pattern, and use the function highlight_file() to show a nice looking code list.

In your httpd.conf you can add the following:

  1 
  2 <Location /source>
  3     ForceType application/x-httpd-php
  4 </Location>
  5        

And then make a file named "source" and put it in your web root directory.

  1 
  2 <HTML>
  3 <HEAD>
  4 <TITLE>Source Display</TITLE>
  5 </HEAD>
  6 <BODY BGCOLOR="white">
  7 <?php
  8     $script = getenv ("PATH_TRANSLATED");
  9     if(!$script) {
 10         echo "<BR><B>ERROR: Script Name needed</B><BR>";
 11     } else {
 12         if (ereg("(\.php|\.inc)$",$script)) {
 13             echo "<H1>Source of: $PATH_INFO</H1>\n<HR>\n";
 14             highlight_file($script);
 15         } else {
 16             echo "<H1>ERROR: Only PHP or include script names are allowed</H1>"; 
 17         }
 18     }
 19     echo "<HR>Processed: ".date("Y/M/d H:i:s",time());
 20 ?>
 21 </BODY>
 22 </HTML>
 23        

Then you can use an URL like the one below to display a colorized version of a script located in "/path/to/script.php" in your web site.

  1 
  2 http://your.server.com/source/path/to/script.php
  3   

See also highlight_string(), show_source().