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("/<!--/i", $split)){
$tmpStr .= '<span style="color:' . $com . ';font-style:italic;">' . $split . '</span>';
}elseif(preg_match("/<style/i", $split)){
//print_r($split);exit;
$spl = preg_split("/(<style.*?>|<\/style>)/i", $split, -1, PREG_SPLIT_DELIM_CAPTURE);
//print_r($spl);
$tmpStr .= preg_replace(array(
'~(\s[a-z].*?=)~',
'~(<([a-z?]|!DOCTYPE).*?>)~'
), 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("~(</[a-zA-Z].*?>)~", '<span style="color:' . $tag . ';">$1</span>', $spl[3]);
}else{
$find = array(
'~(\s[a-z].*?=)~', // Highlight the attributes
'~("[a-zA-Z0-9\/].*?")~', // Highlight the values
'~(<([a-z?]|!DOCTYPE).*?>)~', // Highlight the beginning of the opening tag
'~(</[a-zA-Z].*?>)~', // Highlight the closing tag
'~(&.*?;)~', // Stylize HTML entities
'~(<!DOCTYPE.*?>)~', // 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?