Okay, I'm trying to create a custom syntax highlighting tag for a forum. It works fine with just a few hiccups.

Here's the code:


<?php

$keyword1 = array(
"ACTION_IF",
"AND",
"ELSE",
"END",
"IF",
"NOT",
"OR",
"THEN");

$keyword2 = array(
"AUTHOR");

$string = "[sometag]
AUTHOR ~user@domain.com~
[/sometag]";

if(preg_match("/\[sometag\](.*)\[\/sometag\]/is",$string))
{

echo "It's a match!";

$string = preg_replace("/\[sometag\](.*)\[\/sometag\]/is","\\1",$string);

for($i = 0; $i < count($keyword2); $i++)

{

$string = preg_replace("/".$keyword2[$i]."/","<font color=\"#079D31\">".$keyword2[$i]."</font>",$string);

}

for($i = 0; $i < count($keyword1); $i++)

{

$string = preg_replace("/".$keyword1[$i]."/","<font color=\"#0000A0\">".$keyword1[$i]."</font>",$string);

}

}

$string = nl2br($string);

echo $string;

?>

The problem is I need to match whole words. Because when $string is echoed out at the end, the first four letters of AUTHOR are green, and the last two are blue. It's taking the OR keyword of the first array and matching that to AUTHOR. I've tried using the \b word boundary modifier but when I use that, nothing is highlighted at all.

Anyone have any ideas?

Thanks.

    Okay, back again. I have everything working except one small detail.

    I need to take anything that is in between quotes and is being displayed on the page and highlight that.

    Example:

    
    $string = "I'm stuff highlighted in between quotes";
    
    

    I've tried a few things, but it seems to be picking up the quotes in the actual HTML source as well.

    Are there any ready-made functions out there that does this?

    Thanks again.

      Heh, nevermind, figured that out too. I should really play around 10 minutes more before posting. 🙂

        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("/(&quot;.*?&quot;)/","<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.

          Here's one idea: don't try and do it in place. Identify all the comments, pull them out (leaving placemarkers behind), syntax-highlight the rest, and then stick the comments back in (with appropriate colouring). Or (maybe simpler) do all the highlighting, pull out all the comments, clean their spurious colouring, and put them back in. Either way, it would probably be more straightforward than allatime checking to check if we're in a comment or not...

          I've sketched this idea out a couple of times in the past on this forum; because I'm short for time I'll just link to those discussions:

          1
          2

          Now the only thing that worries me is a comment like / This comment contains "a / CSS COMMENT /" within it /...

          I think pulling strings before comments, styling the string and comment placemarkers (which means that the string placemarkers that are inside comments don't get styled), then putting back comments and then putting back strings, would be the order to do things.

            Write a Reply...