Subscribe to Tutorial Feed

Flash and PHP Bible

The Flash and PHP Bible has been released! The book can be found on Amazon or wherever fine books are sold in your area.

The Flash and PHP Bible has a forum for quick support.

Scriptplayground » tutorials » php » Highlight PHP Code

Highlight PHP Code

Highlight your PHP code making it easier to read.

We will be able to achieve this code highlighting fairly simple using a PHP function, highlight_string().

The highlight_string() function takes two arguments "parameters". The first is the code to highlight and the second if set to true will return a highlighted string instead of the printing it to the screen. This gives you the ability to feed it into a file or maybe pass it into Flash and display it within your Flash application.

This is a sample use of that function, like I said this is a very simple concept but makes it very easy to view your code

$codeToHighlight = "<?php
function validLink(\$link) {
    if(preg_match(\"/http:\/\//\", \$link)) {
        return true;
    } else {
        return false;


Here is the result of that example, the output in PHP4 is not XHTML compliant, but of course you could fix that. More on that in a moment. In this example we need to escape (add a \) to quotes " and dollar signs $. Otherwise the PHP parser will actually read the variables and hide them from the code highlighter.

The HTML Source, not pretty but that is what you get with an automatic parser.

<code><font color="#000000">
<font color="#0000BB"><?php<br />
</font><font color="#007700">function </font>
<font color="#0000BB">validLink</font><font color="#007700">(</font>
<font color="#0000BB">$link</font><font color="#007700">) {
<br />    if(</font><font color="#0000BB">preg_match
</font><font color="#007700">(</font><font color="#DD0000">"/http:\/\//"
</font><font color="#007700">, </font><font color="#0000BB">$link
</font><font color="#007700">)) { 
<br />        return </font>&
lt;font color="#0000BB">true</font><font color="#007700">;
<br />    } else {
<br />        return </font>
<font color="#0000BB">false</font><font color="#007700">;
<br />    }
<br />}
<br /></font><font color="#0000BB">?></font>

The highlighted result as the browser displays it.

function validLink($link) {
    if(preg_match("/http:\/\//", $link)) {
        return true;
    } else {
        return false;

As you can see the code is much easier on the eyes and allows you to quickly overlook bits of code online and achieve a similar found within most code editors.

An alternative to this function would be to use a prewritten library that highlights your code for you, one example script is GEHSI a great all around code highlighter for many popular programming languages.

That is all there is to know about highlighting your code. Until next time, happy coding.

Follow Scriptplayground on Twitter (@scriptplay)

Comments: Highlight PHP Code

 php manual  Sat Jan 27, 2007 2:59 am  
very good article,thanks
 Cody Fisher  Sat Apr 21, 2007 5:11 am  
To get rid of the font tags, you could always use

str_replace("", "");

for each font tag, to make the different colors, and this

", "");

to replace all the /font tags with /span tags.

I may decide to use this, great tutorial.
 Cody Fisher  Sat Apr 21, 2007 5:15 am  
Sorry for double-posting. I just checked and it looks like PHP5 automatically uses span instead of font tags. So if you use PHP4 there you go (above).
 mkeefe  Sat Apr 21, 2007 8:17 am  
Hey Cody, you also could use preg_replace() to match all the font tags instead of creating multiple str_replace() calls.
 Smith  Mon Jun 29, 2009 6:44 am  
This is a very easy to use code highlighter
Add a comment
The ability to add new comments has been disabled in this archived version.

Main | Tutorials | Articles