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;
}
}
?$gt;";
highlight_string($codeToHighlight);
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>
</font>
</code>
The highlighted result as the browser displays it.
<?php
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.