Categories
PHP

Syntax Highlighting Using PHP

You can publish Syntax Highlighted PHP code easily using php’s built in functions:

  • highlight_string()
  • highlight_file()

 

<?php
highlight_string
(‘<?php phpinfo(); ?>’);
?>

The above code will generate the following HTML codes:

<code><font color=”#000000″>

<font color=”#0000BB”>&lt;?php phpinfo</font><font color=”#007700″>(); </font><font color=”#0000BB”>?&gt;</font>

</font>

</code>

 

As you can guess from the function name, highlight_file() takes a filename/path as a string parameter and returns the code.

 

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