Okay, this one I can't figure out at all and I've been playing around with it for hours.
I have the syntax highlighting working just fine, with stuff between / and / coloured for comments as well as //.
However, my problem is this: If something in between the block comment or line comment is a recognized syntax word, it is getting coloured to the appropriate syntax colour.
I want it so that if any syntax word is in between a block comment or after a line comment, then it will not be coloured, it will remain grey. (I hope that makes sense).
So, to demonstrate with PHP's great syntax highlighting, this is what I'm trying to accomplish:
<?php // Ooh looks it's blue!
// <?php now it's orange!
?>
I've tried fumbling around with assertions and negative assertions with no luck. My understanding of PCRE is increasing, but I just can't grasp negative assertions.
Here's my code thus far:
<?php
$keyword1 = array(
"ACTION_IF",
"AND",
"ELSE",
"END",
"IF",
"NOT",
"OR",
"THEN");
$keyword2 = array(
"ADD_KIT",
"ADD_MUSIC",
"ADD_STATE_TRIGGER",
"ADD_TRANS_TRIGGER",
"ALLOW_MISSING",
"APPEND",
"APPENDI"); // There's tonnes more, but I cut is short.
$keyword3 = array(
"2HANDED",
"2WEAPON",
"ABAZIGAL_SHOCKWAVE",
"ABJURER",
"ACCRUSHINGMOD",
"ACID",
"ACID",
"ACID_DAMAGE_1"); // Again tonnes more, but cut short.
$string = "[sometag]
// This is a 2HANDED comment.
/* Blah, blah,
IF ELSE OR NOT */
[/sometag]";
$string = htmlspecialchars($string);
if(preg_match("/\[sometag\](.*)\[\/sometag\]/is",$string))
{
$string = preg_replace("/\[sometag\](.*)\[\/sometag\]/is","<span class=\"courier\">\\1</span>",$string);
echo "It's a match!";
for($i = 0; $i < count($keyword3); $i++)
{
$string = preg_replace("/(\b".$keyword3[$i]."\b)/","<span style=\"color: #0000FF;\">".$keyword3[$i]."</span>",$string);
}
for($i = 0; $i < count($keyword2); $i++)
{
$string = preg_replace("/(\b".$keyword2[$i]."\b)/","<span style=\"color: #079D31;\">".$keyword2[$i]."</span>",$string);
}
for($i = 0; $i < count($keyword1); $i++)
{
$string = preg_replace("/(\b".$keyword1[$i]."\b)/","<span style=\"color: #0000A0;\">".$keyword1[$i]."</span>",$string);
}
$string = preg_replace("/(".*?")/","<span style=\"color: #C30B2E\">\\1</span>",$string);
$string = preg_replace("/(\/\*.*?\*\/)/is","<span style=\"color: #A8A4A4\">\\1</span>",$string);
$string = str_replace("~","<span style=\"color: #B0B0C2;\">~</span>",$string);
}
$string = nl2br($string);
$string = preg_replace("/(\/\/.*)/","<span style=\"color: #A8A4A4\">\\1</span>",$string);
echo "<html>
<head>
<title>This is a title.</title>
<style type=\"text/css\">
<!--
.courier {
font-family: Courier New;
font-size: 10pt;
}
-->
</style>
</head>";
echo $string;
So, the string comes out looking like this:
// This is a 2HANDED comment.
/ Blah, blah,
IF ELSE OR NOT /
And I want it to look like this:
// This is a 2HANDED comment.
/ Blah, blah,
IF ELSE OR NOT /
I just need to figure out someway in those three for loops to exclude a match if / is present before the match and / is present after. Also if // is present before the match on the same line.
The // one seems easy, but again, I'm not that good with assertions. 😉
Oh, and if you see anything in the code that should have a \ but doesn't, that's because of PHP's syntax highlighting. 😉
Thanks for any help.