





This tutorial will show you how to take a URL, read in the source CODE and then format it.
The code listed will assume that you are passing the URL as an encoded variable via GET with the name u.
First off here are the CSS styles you'll need to format the code (you should customise these to suit your needs):
Now we needs to decode the URL and use it to read the HTML in via an HTTP file stream. This is very easy to do, we just use PHP's file() function.
No we've got the HTML stored in a variable we need to use htmlspecialchars() to make the source code display the same way through the browser as it would look like if we were to view the source. We then do some string replacement to insert out CSS styles into the HTML so that the tags and the tag contents are different colours:
That's all there is to it! If you saved this page as viewsource.php then you'd link to it like this:
<a href="viewsource.php?u=<?= $_SERVER[ "PHP_SELF" ] ?>">View Source</a>
These resources are provided free of charge. If, however, you would like to make a donation to help finance these resources, no matter how big or small, please use the PayPal button below.
This resource was written by Tim Bennett Bsc (Hons). Tim Bennett has a First Class Degree in Multimedia Systems Computing from Leeds Metropolitan University. He works as a freelance multimedia developer under the company name Texelate creating free and commissioned high-quality multimedia solutions. Find out more by visiting the portfolio section and viewing his blog. If you'd like to request a resource get in touch!









