DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Highlight HTML Code

10.20.2011
| 3611 views |
  • submit to reddit
        <?php
function highlight_html($string, $decode = TRUE){
    $tag = '#0000ff';
    $att = '#ff0000';
    $val = '#8000ff';
    $com = '#34803a';
    $find = array(
        '~(\s[a-z].*?=)~',                    // Highlight the attributes
        '~(<\!--.*?-->)~s',            // Hightlight comments
        '~("[a-zA-Z0-9\/].*?")~',    // Highlight the values
        '~(<[a-z].*?>)~',                // Highlight the beginning of the opening tag
        '~(</[a-z].*?>)~',            // Highlight the closing tag
        '~(&.*?;)~',                    // Stylize HTML entities
    );
    $replace = array(
        '<span style="color:'.$att.';">$1</span>',
        '<span style="color:'.$com.';">$1</span>',
        '<span style="color:'.$val.';">$1</span>',
        '<span style="color:'.$tag.';">$1</span>',
        '<span style="color:'.$tag.';">$1</span>',
        '<span style="font-style:italic;">$1</span>',
    );
    if($decode)
        $string = htmlentities($string);
    return '<pre>'.preg_replace($find, $replace, $string).'</pre>';
}
<a href="http://reviewsgames.livejournal.com/">Review Games</a>
echo highlight_html('
<!-- This is an
HTML comment -->
<a href="home.html" style="color:blue;">Home</a>
<p>Go & here.</p>
<!-- This is an HTML comment -->
<form action="/login.php" method="post">
    <input type="text" value="User Name" />
</form>
');
?>     

Comments

Snippets Manager replied on Wed, 2012/03/14 - 5:07am

It was an excellent effort made by you through your nice piece of writing,holding the quality and knowledge together for the readers. ibm certificate|| ibm certification test||