www.xbdev.net
xbdev - software development
Monday May 4, 2026
Home | Contact | Support | PHP... a powerful, flexible, fully supported, battle tested server side language ..
     
 

PHP...

a powerful, flexible, fully supported, battle tested server side language ..

 


Highlighting (Making Code (Look) Beautiful)


There are lots of packages and tool for color highlighting your code - in lots of different languages - you can even write your own. However, did you know that there is a built in string highlighter in PHP? The highligher is simple, fast and very effective - it produces syntax highlighted (HTML) code!


<?php
$aa = "<?php
for i = 2;
let b = 2;
b = 2 * 4;
// comment var

' ddd s var a= 2;' 
tesr
";

$bb = highlight_string( $aa, true );
echo( $bb );


The above code will produce this output:

<code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php
<br /></span><span style="color: #007700">for&nbsp;</span><span style="color: #0000BB">i&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;
<br /></span><span style="color: #0000BB">let&nbsp;b&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;
<br /></span><span style="color: #0000BB">b&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">2&nbsp;</span><span style="color: #007700">*&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">;
<br /></span><span style="color: #FF8000">//&nbsp;comment&nbsp;var
<br />
<br /></span><span style="color: #DD0000">'&nbsp;ddd&nbsp;s&nbsp;var&nbsp;a=&nbsp;2;'&nbsp;
<br /></span><span style="color: #0000BB">tesr
<br /></span>
</span>
</code>


Just a small warning about the additional argument
true
- if this isn't set - the output from the
highlight_string
function will be passed directly to the output instead of being returned as a string.

This is what the colorful code would look like in a browser:

<?php
for 2;
let b 2;
4;
// comment var

' ddd s var a= 2;' 
tesr


Stripping off the
<?php
and
<code>
Tags


We'll assume the string always has this at the start and end - so we can find the offsets and get the substring.

// strip off the '<?php' and 
$bb = substr( $bb, strpos($bb, 'php') + 4 + 6 + 7 );     // start php  '4' is for 'php' and arrows, 7 is for '<br />', '7' is for the span
$bb = substr( $bb, 0, strrpos($bb, '</span>') ); // end 


The above example would change to this with these extra two lines:

<span style="color: #007700">for&nbsp;</span><span style="color: #0000BB">i&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;
<br /></span><span style="color: #0000BB">let&nbsp;b&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;
<br /></span><span style="color: #0000BB">b&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">2&nbsp;</span><span style="color: #007700">*&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">;
<br /></span><span style="color: #FF8000">//&nbsp;comment&nbsp;var
<br />
<br /></span><span style="color: #DD0000">'&nbsp;ddd&nbsp;s&nbsp;var&nbsp;a=&nbsp;2;'&nbsp;
<br /></span><span style="color: #0000BB">tesr
<br /></span>


Of course, the pretty colorful version (what it looks like in the output):

for 2;
let b 2;
4;
// comment var

' ddd s var a= 2;' 
tesr


Helper Function


Taking the notes into consideration we can construct a simple highlight function that takes a string and returns the highlighted code (without the additional code span tags and the php delimiter).

function highlight($code)
{
    $bbbcode  = "<?php" . $code; 
    $bbbcolor = highlight_string( $bbbcode, true );
    // start php  '4' is for 'php' and arrows - no newline - so the 5 is for the span
    $bbbcolor = substr( $bbbcolor, strpos($bbbcolor, 'php') + 4 + 6 );     
    // subtrack  - so it removes the span at end
    $bbbcolor = substr( $bbbcolor, 0, strrpos($bbbcolor, '</span>') ); // end 
    return '<div style="background-color:rgba(100,100,250,0.1);min-height:4px;">' . $bbbcolor . '</div>';
}



Other Resources


Highlighting using Regular Expressions
• Notepad++ (Export Color Highlighting)
Notebook Example HighlightJS
JS HighlightJS Library (Free JS Library)
Prism Highlighter






















 
Advert (Support Website)

 
 Visitor:
Copyright (c) 2002-2026 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.