1

I have the following function to highlight HTML syntax.

public function highlightHTML($content){
    $iscomment = false;
    $tag       = "#0000ff";
    $att       = "#ff0000";
    $val       = "#8000ff";
    $com       = "#34803a";
    $doc       = "#bf9221";
    $tmpStr    = "";
    $sp        = preg_split('/(<!--.*?-->|<style.*?>.*?<\/style>)/s', $content, -1, PREG_SPLIT_DELIM_CAPTURE);
    foreach($sp as $split){
        $split = htmlentities($split, ENT_QUOTES, "UTF-8", false);
        if(preg_match("/&lt;!--/i", $split)){
            $tmpStr .= '<span style="color:' . $com . ';font-style:italic;">' . $split . '</span>';
        }elseif(preg_match("/&lt;style/i", $split)){
            //print_r($split);exit;
            $spl = preg_split("/(&lt;style.*?&gt;|&lt;\/style&gt;)/i", $split, -1, PREG_SPLIT_DELIM_CAPTURE);
            //print_r($spl);
            $tmpStr .= preg_replace(array(
                '~(\s[a-z].*?=)~',
                '~(&lt;([a-z?]|!DOCTYPE).*?&gt;)~'
                    ), array(
                '<span style="color:' . $att . ';">$1</span>',
                '<span style="color:' . $tag . ';">$1</span>'
                    ), $spl[1]);
            //$tmpStr .= $this->highlight($spl[2], HIGHLIGHT_CSS);
            $tmpStr .= preg_replace("~(&lt;/[a-zA-Z].*?&gt;)~", '<span style="color:' . $tag . ';">$1</span>', $spl[3]);
        }else{
            $find    = array(
                '~(\s[a-z].*?=)~', // Highlight the attributes
                '~(&quot;[a-zA-Z0-9\/].*?&quot;)~', // Highlight the values
                '~(&lt;([a-z?]|!DOCTYPE).*?&gt;)~', // Highlight the beginning of the opening tag
                '~(&lt;/[a-zA-Z].*?&gt;)~', // Highlight the closing tag
                '~(&amp;.*?;)~', // Stylize HTML entities
                '~(&lt;!DOCTYPE.*?&gt;)~', // DOCTYPE
            );
            $replace = array(
                '<span style="color:' . $att . ';">$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>',
                '<span style="color:' . $doc . ';">$1</span>',
            );
            $tmpStr .= preg_replace($find, $replace, $split);
        }
        $iscomment = !$iscomment;
    }
    return $tmpStr;
}

Currently if the function encounters html tags inside <script> ... </script>, these tags are highlighted. I would like this function to ignore (not highlight) everything what's inside <script>...</script>. Also I would like the function to match <script></script> as well as <script ... "></script>. How to modify this function to achieve this?

0 Answers0