Click to See Complete Forum and Search --> : PHP syntax highlighting


Da Warriah
04-21-2003, 08:01 PM
ok, im helping one of my friends out with a website about C# (which i know nothing about, im just the webmaster, lol)...anyways, for his tutorials, he wants some syntax highlighting, like changing the colors for different keywords...this is to be done in PHP, and i have the basic script laid out, but im stumped with one thing: comments...
everythings fine with multiline comments, such as

/* this is a
multiline
comment */

but when i get to one line comments, i cant think for the life of me, how to end the highlighting at the end of the line...heres my code so far:

$tut = str_replace("//","<span class=\"kw2\">//",$tut);
$tut = str_replace("<||>","</span>",$tut);

that works fine, but every time someone wants to write a comment, they will have to put a <||> at the end of it...which is not what i want to happen...so is there any way to replace a line break with a </span> tag, but ONLY after it has replaced the double slash?? any help would be GREATLY appreciated here...

Jona
04-21-2003, 10:53 PM
I haven't tried it and I don't know if it will work, but... Try using \n instead of <||>

Da Warriah
04-22-2003, 03:57 PM
well i was going to do that, and it would work, but the problem is, it would add a </span> at the end of EVERY line...which isnt what i want, i only want at the end of the lines where theres a comment:(

Jona
04-22-2003, 03:59 PM
I haven't tried it and I don't know if it will work....

Like I said..... Sorry.

AdamGundry
04-22-2003, 04:14 PM
How about this:

<?
$lns = explode("\n", $tut);
$tut = "";

foreach ($lns as $line){
if (substr_count($line, "//") > 0){
$line = str_replace("//","<span class=\"kw2\">//",$line) . "</span>\n";
$tut .= $line;
} else {
$tut .= $line . "\n";
}
}
?>


I think this should do what you want. It will, however, fall over if more than two forward slashes are used on one line.

Adam

Jona
04-22-2003, 04:21 PM
Hey, Adam, would you mind explaining out the substr_count() function?

AdamGundry
04-22-2003, 04:28 PM
It's simple. Substr_count() takes a string and a substring to search for, then returns the number of times substring occurs within string.

See the PHP manual:
http://www.php.net/manual/en/function.substr-count.php

Adam

Jona
04-22-2003, 04:38 PM
Hmmm.... I see. Thanks, again. :p You're all over the forums today... :)

Da Warriah
04-22-2003, 09:39 PM
wow thanx...of course i really have barely any idea what it does, but from what i can get out of it, it seems like itll work, thanx:)

(and i dont think itll be a problem with more than two // on a line...its comments - you only need one per line:p )