Re: Syntax highlighting of odd language

From: Date: Fri, 05 Nov 2004 21:32:44 +0000
Subject: Re: Syntax highlighting of odd language
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-201330@lists.php.net to get a copy of this message
Aaron Gould wrote:
Bruno B B Magalhães wrote:
$code = 'function what() { do oddname; %oddsyntax }'; function highlight_code($code) {
    $oddsyntax = array('oddsyntax','oddsyntax2','oddsyntax3');
        for($i = 0; $i <= count($oddsyntax)-1; $i++)
    {
        $highlighted_code = eregi_replace($oddsyntax[$i],'<spam class="highlighted">'.$oddsyntax[$i].'</spam>',$code);
    }
    return $code;
}
Hi Bruno... I tried your sample, but it doesn't seem to work. I do however see what you're getting at. Perhaps I need to experiment with the eregi_replace commands (or other regex commands) a bit. Thanks,
for what Bruno showed you, you definitly don't need regexps. That example doesn't use regular expressions in case you didn't notice, so would work just as well with a simple str_replace. Now, the problem with such a "solution" is the following. Imagine you have the following keywords: include require in of typof now, when you replace include with <span class="highlighted">include</span>, it'll go on, and also replace all instances of in, so you'll end up with things like <span class="highlighted"><span class="highlighted">in</span>clude</span> (very ugly). *a* way to get around that is by using word boundries around the keyword to be replaced (eg. preg_replace('\(\W|^)+('.$highlight[$i].')(\W|$)\i', '$1<span class="highlight">$2</span>$3') )

« previous php.general (#201330) next »