Jump to content

Highlight (color code) Your PHP with the .phps Extension


Byron

Recommended Posts

If your lke me, sometimes you need to show the source of your php script. You can highlight (color code) your php code with the highlight_file() function. By using the .phps extension you can just make a copy of the php file and then rename it with the phps extension like below:

 

Working file:

http://helio.byrondallas.heliohost.org/server_load_s.gif

 

Highlighted file:

http://helio.byrondallas.heliohost.org/server_load_s.phps

 

First create a file named phpsource.php in your root directory and add the code below:

 

<?php
if (substr($_GET['file'],strpos($_GET['file'],'.')) == '.phps') {
highlight_file($_GET['file']);
}
?>

 

Then add these two lines to your root .htaccess file:

 

RewriteEngine On
RewriteRule ^(.+.phps)$ phpsource.php?file=$1 [L]

 

Now you can show the source of any php file by adding the .phps extension.

 

There's also another way which I use more often and that's using the highlight_string() function.

 

<?php
highlight_string('YOUR PHP CODE HERE');
?>

 

I normally just add that to the bottom of an example page, like the example below:

 

http://helio.byrondallas.heliohost.org/email_form.php

 

Just remember if you use single quotes to wrap your code in, you'll need backslash ALL single quotes inside your code \'.

 

Same way if you use double quotes to wrap your code in, then you'll need to backslash ALL of the double quotes in your code \".

 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...