Witam,
Mam funkcje kolorowania składni php z coyot'a 0.9.0 (chyba albo 0.9.1) i nieco ją przerobiłem ale mam problem z " a mianowicie:
jeśli mam takie coś:
<font color="#007700">$text</font> = <font color="#ff0000">"jakiś sobie tekst w całości pokolorowany"</font>;
to mi go poprawnie koloruje ale jeśli w kodzie znajdzie się " to już zaczynają sie schodzy:
<font color="#007700">$text</font> = <font color="#ff0000">"jakiś sobie tekst pokolorowany \"</font>ten tekst nie jest pokolorowany \<font color="#ff0000">"a ten już jest"</font>;
i jak to teraz poprawić by ignorował znaki " poprzedzone \ ????
oto f-cja:
function PHPHighlight($body) {
global $PHP_RESERVED;
//wyodrębnienie zawartosci calego tekstu z wnetrza <php></php>
preg_match_all( "|<php>(.*?)</php>|si", $body, $matched );
for( $i = 0; $i < count( $matched[1] ); $i++ ) {
//usuwanie znacznikow HTML
$html = htmlspecialchars( $matched[1][$i], ENT_NOQUOTES );
$html = str_replace( " ", "&nbsp;", $html );
//dzielenie tokenów komentarzy i stringów # /* */ // ' " ` '
$tokens = preg_split( "(\#|/\*|\*/|'|".'"'."|`|\\n|//)", $html );
$tokens_count = count( $tokens );
preg_match_all( "(\#|/\*|\*/|'|".'"'."|`|\\n|//)", $html, $separators );
$result = "";
//teraz dla tokenów jazda !!
for( $j = 0; $j < $tokens_count; $j++ ) {
//pogrubianie slów kluczowych
$token = $tokens[$j];
for( $w = 0; $w < count( $PHP_RESERVED ); $w++ ) {
$token = preg_replace( "/\b$PHP_RESERVED[$w]\b/i", "<font color=\"#0000ff\">$PHP_RESERVED[$w]</font>", $token );
}
//kolorowanie liczb
$token = preg_replace( "/\b([0-9]+)\b/i", "<font color=\"#BBAA11\">\\1</font>", $token );
//kolorowanie zmiennych
$token = preg_replace( "/(\\$[0-9a-zA-Z_]+)/i", "<font color=\"#007700\">\\1</font>", $token );
$token = preg_replace( "/(\\$\\{.+\\})/i", "<font color=\"#ff00ff\">\\1</font>", $token );
$result .= $token;
//sprawdzanie komentarzy: komentarze i stringi sa parsowane BEZ sprawdzania słów kluczowych wewnątrz
switch( $separators[0][$j] ) {
case "/*": {
$result .= "<font color=\"c0c0c0\"><i>/*";
while( ( $j < $tokens_count - 1 ) && ( $separators[0][++$j] != "*/" ) ) {
$result .= $tokens[$j].$separators[0][$j];
}
$result .= $tokens[$j].$separators[0][$j]."</i></font>";
break;
}
case "#": {
$result .= "<font color=\"c0c0c0\"><i>#";
while( ( $j < $tokens_count - 1 ) && ( $separators[0][++$j] != "\n" ) ) {
$result .= $tokens[$j].$separators[0][$j];
}
$result .= $tokens[$j].$separators[0][$j]."</i></font>";
break;
}
case "//": {
$result .= "<font color=\"c0c0c0\"><i>//";
while( ( $j < $tokens_count - 1 ) && ( $separators[0][++$j] != "\n" ) ) {
$result .= $tokens[$j].$separators[0][$j];
}
$result .= $tokens[$j].$separators[0][$j]."</i></font>";
break;
}
case "'": {
$result .= "<font color=\"ff0000\">'";
while( ( $j < $tokens_count - 1 ) && ( $separators[0][++$j] != "'" ) ) {
$result .= $tokens[$j].$separators[0][$j];
}
$result .= $tokens[$j].$separators[0][$j]."</font>";
break;
}
case "\"": {
$result .= "<font color=\"ff0000\">\"";
while( ( $j < $tokens_count - 1 ) && ( $separators[0][++$j] != "\"" ) ) {
$result .= $tokens[$j].$separators[0][$j];
}
$result .= $tokens[$j].$separators[0][$j]."</font>";
break;
}
case "\`": {
$result .= "<font color=\"ff0000\">\`";
while( ( $j < $tokens_count - 1 ) && ( $separators[0][++$j] != "\`" ) && ( $separators[0][$j] != "\n" ) ) {
$result .= $tokens[$j].$separators[0][$j];
}
$result .= $tokens[$j].$separators[0][$j]."</font>";
break;
}
default: $result .= $separators[0][$j];
}
}
$body = str_replace('<php>' . $matched[1][$i] . '</php>', '<font face="Courier New">' . $result. '</font>', $body);
$body = str_replace('&amp;', '&', $body);
}
return $body;
}