highlight-html-en-xml

Gesponsorde koppelingen

PHP script bestanden

  1. highlight-html-en-xml

« Lees de omschrijving en reacties

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php

    $my_html_string
= 'your html string';
    echo highlight_html($my_html_string);


    // Highlight HTML code
    function highlight_html($string, $bAddDefaultCss = false)
    {

        $result = '';

        if($bAddDefaultCss)
        {

            $result .= '<style type="text/css">'
            . 'code.html { background: #FCFCFC; border: #E8E8E8 solid 1px; color: #000000; display: block; overflow: auto; padding: 20px; white-space: nowrap; }'
            . 'code.html .tag { color: #0000FF; }'
            . 'code.html .comment { color: #008000; }'
            . 'code.html .attribute { color: #FF0000; }'
            . 'code.html .value { color: #FF00FF; }'
            . '</style>';
        }


        $expr = '/<!--((\n|.)*?)-->|<[a-z0-9]+(([\n\s]+)([a-z0-9\-]+)([\n\s]*)=([\n\s]*)"([^"]*)")*([\n\s]*\/)?>|<\/[a-z0-9]+>|(.)/i';
        $matches = array();

        preg_match_all($expr, $string, $matches);
        // print_r($matches[0]);
        $bAsValue = false;

        for($i = 0; $i < sizeof($matches[0]); $i++)
        {

            if(strcasecmp($match = $matches[0][$i], "") !== 0)
            {

                if(preg_match('/<!--((\n|.)*?)-->/i', $match)) // Comment
                {
                    $result .= '<span class="comment">' . stringToHtml($match) . '</span>';
                }

                elseif(preg_match('/<\/[a-z0-9]+>/i', $match)) // Closing tag
                {
                    $result .= '<span class="tag">' . stringToHtml($match) . '</span>';
                }

                elseif(preg_match('/<[a-z0-9]+(([\n\s]+)([a-z0-9\-]+)([\n\s]*)(=([\n\s]*)"([^"]*)")?)*([\n\s]*\/)?>
/i', $match)) // Opening tag
{
$expr2 = '/(<[a-z0-9]+)|([a-z0-9\-]+)|(=)|("([^"]*)")|(\/>)|(.)/i';
$matches2 = array();
preg_match_all($expr2, $match, $matches2);

$result .= '<span class="tag">' . stringToHtml($matches2[0][0]) . '</span>';

for($j = 1; $j < sizeof($matches2[0]); $j++)
{
if(strcasecmp($match2 = $matches2[0][$j], "") !== 0)
{
if(strcasecmp("/>", $match2) === 0)
{
$result .= '<span class="tag">/&gt;</span>';
}
elseif(strcasecmp(">", $match2) === 0)
{
$result .= '<span class="tag">&gt;</span>';
}
elseif(strcasecmp("=", $match2) === 0)
{
$bAsValue = true;
$result .= '=';
}
else
{
if(strcasecmp(" ", $match2) === 0)
{
$result .= "&nbsp;";
}
elseif(strcasecmp("\t", $match2) === 0)
{
$result .= "&nbsp;&nbsp;&nbsp;&nbsp;";
}
elseif(strcasecmp("\r", $match2) === 0)
{
// Ignore
}
elseif(strcasecmp("\n", $match2) === 0)
{
$result .= "<br>";
}
else
{
if($bAsValue)
{
$result .= '<span class="value">' . stringToHtml($match2) . '</span>';
}
else
{
$result .= '<span class="attribute">' . stringToHtml($match2) . '</span>';
}
}

$bAsValue = false;
}
}
}
}
else // No tag
{
$result .= stringToHtml($match);
}
}
}

return '<code class="html">' . $result . '</code>';
}

?>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.