Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> problem z " i \", skrypt kolorowania składni php
angel2953
post
Post #1





Grupa: Zarejestrowani
Postów: 199
Pomógł: 5
Dołączył: 8.07.2004
Skąd: gdynia

Ostrzeżenie: (0%)
-----


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ś:
Kod
$text = "jakiś sobie tekst w całości pokolorowany";

to mi go poprawnie koloruje ale jeśli w kodzie znajdzie się \" to już zaczynają sie schodzy:
Kod
$text = "jakiś sobie tekst pokolorowany \"ten tekst nie jest pokolorowany \"a ten już jest";

i jak to teraz poprawić by ignorował znaki " poprzedzone \ questionmark.gifquestionmark.gifquestionmark.gifquestionmark.gif
oto f-cja:
  1. <?php
  2.  
  3. function PHPHighlight($body) {
  4. global $PHP_RESERVED;
  5. //wyodrębnienie zawartosci calego tekstu z wnetrza <php></php>
  6. preg_match_all( &#092;"|<php>(.*?)</php>|si\", $body, $matched );
  7. for( $i = 0; $i < count( $matched[1] ); $i++ ) {
  8. //usuwanie znacznikow HTML
  9. $html = htmlspecialchars( $matched[1][$i], ENT_NOQUOTES );
  10. $html = str_replace( &#092;" \", \"&nbsp;\", $html );
  11. //dzielenie tokenów komentarzy i stringów # /* */ // ' \" ` '
  12. $tokens = preg_split( &#092;"(#|/*|*/|'|\".'\"'.\"|`|n|//)\", $html );
  13. $tokens_count = count( $tokens );
  14. preg_match_all( &#092;"(#|/*|*/|'|\".'\"'.\"|`|n|//)\", $html, $separators );
  15. $result = &#092;"\";
  16. //teraz dla tokenów jazda !!
  17. for( $j = 0; $j < $tokens_count; $j++ ) {
  18. //pogrubianie slów kluczowych
  19. $token = $tokens[$j];
  20. for( $w = 0; $w < count( $PHP_RESERVED ); $w++ ) {
  21. $token = preg_replace( &#092;"/b$PHP_RESERVED[$w]b/i\", \"<font color=\"#0000ff\">$PHP_RESERVED[$w]</font>\", $token );
  22. }
  23. //kolorowanie liczb
  24. $token = preg_replace( &#092;"/b([0-9]+)b/i\", \"<font color=\"#BBAA11\">1</font>\", $token );
  25. //kolorowanie zmiennych
  26. $token = preg_replace( &#092;"/($[0-9a-zA-Z_]+)/i\", \"<font color=\"#007700\">1</font>\", $token );
  27. $token = preg_replace( &#092;"/(${.+})/i\", \"<font color=\"#ff00ff\">1</font>\", $token );
  28. $result .= $token;
  29. //sprawdzanie komentarzy: komentarze i stringi sa parsowane BEZ sprawdzania słów k
    luczowych wewnątrz
  30. switch( $separators[0][$j] ) {
  31. case &#092;"/*\": {
  32.  $result .= &#092;"<font color=\"c0c0c0\\"><i>/*\";
  33.  while( ( $j < $tokens_count - 1 ) && ( $separators[0][++$j] != &#092;"*/\" ) ) {
  34.  $result .= $tokens[$j].$separators[0][$j];
  35.  }
  36.  $result .= $tokens[$j].$separators[0][$j].&#092;"</i></font>\";
  37.  break;
  38.  }
  39. case &#092;"#\": {
  40. $result .= &#092;"<font color=\"c0c0c0\\"><i>#\";
  41. while( ( $j < $tokens_count - 1 ) && ( $separators[0][++$j] != &#092;"n\" ) ) {
  42. $result .= $tokens[$j].$separators[0][$j];
  43. }
  44. $result .= $tokens[$j].$separators[0][$j].&#092;"</i></font>\";
  45. break;
  46. }
  47. case &#092;"//\": {
  48.  $result .= &#092;"<font color=\"c0c0c0\\"><i>//\";
  49.  while( ( $j < $tokens_count - 1 ) && ( $separators[0][++$j] != &#092;"n\" ) ) {
  50.  $result .= $tokens[$j].$separators[0][$j];
  51.  }
  52.  $result .= $tokens[$j].$separators[0][$j].&#092;"</i></font>\";
  53.  break;
  54.  }
  55. case &#092;"'\": {
  56. $result .= &#092;"<font color=\"ff0000\\">'\";
  57. while( ( $j < $tokens_count - 1 ) && ( $separators[0][++$j] != &#092;"'\" ) ) {
  58. $result .= $tokens[$j].$separators[0][$j];
  59. }
  60. $result .= $tokens[$j].$separators[0][$j].&#092;"</font>\";
  61. break;
  62. }
  63. case &#092;"\"\": {
  64. $result .= &#092;"<font color=\"ff0000\">\"\";
  65. while( ( $j < $tokens_count - 1 ) && ( $separators[0][++$j] != &#092;"\"\" ) ) {
  66. $result .= $tokens[$j].$separators[0][$j];
  67. }
  68. $result .= $tokens[$j].$separators[0][$j].&#092;"</font>\";
  69. break;
  70. }
  71. case &#092;"`\": {
  72.  $result .= &#092;"<font color=\"ff0000\\">`\";
  73.  while( ( $j < $tokens_count - 1 ) && ( $separators[0][++$j] != &#092;"`\" ) && ( $separators[0][$j] != \"n\" ) ) {
  74.  $result .= $tokens[$j].$separators[0][$j];
  75.  }
  76.  $result .= $tokens[$j].$separators[0][$j].&#092;"</font>\";
  77.  break;
  78.  } 
  79. default: $result .= $separators[0][$j];
  80. }
  81. }
  82. $body = str_replace('<php>' . $matched[1][$i] . '</php>', '<font face=\"Courier New\">' . $result. '</font>', $body);
  83. $body = str_replace('&amp;', '&', $body);
  84. }
  85. return $body;
  86. }
  87.  
  88. ?>


powyższa f-cja pochodzi z Coyot'a


--------------------
Sygnaturkę ukradli
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 19.08.2025 - 11:33