Seite 1 von 1

tabs berücksichtigen

Verfasst: 14.02.2007 17:10
von Ambience
Hallo, ich habe folgenden Code:

Code: Alles auswählen

<?php

function highlight_php($code)
{ 
  $h_php = $code;
  return $h_php;
}
$text = "<?php

//
// testkommentar
//
/* noamal */

abstract
&=
function(\$test)
{
    \$code = \$test;
  
    echo \$code;
}

\$variable = array(\"testtext\" => 0);

?>";

echo "<span style=\"font-family:'Courier New';font-size:10pt;\">" . nl2br(highlight_php($text)) . "</span>";

?>
leider wird der Text bzw. der Code nicht eingerückt ausgegeben. Wie kann man die Tabs berücksichtigen?

Verfasst: 14.02.2007 17:32
von punkface

Verfasst: 14.02.2007 17:52
von Ambience
kannst du mal mein script testen und mir sagen wieso das dann auf einmal so verzehrt aussieht?

Code: Alles auswählen

<?php

function highlight_php($code)
{ 
  $h_php = $code;
  $script = $code;
  $tokens = token_get_all($script); 
  
  foreach($tokens as $token)
  {
    if (!is_array($token))
    {
      //
      // auf alles andere pr&uuml;fen
      //
    }
    else
    {
      list($id, $tok) = $token;
      
      switch(token_name($id))
      {
        case 'T_ABSTRACT' :
        case 'T_ARRAY' :
        case 'T_ARRAY_CAST' :
        case 'T_AS' :
        case 'T_BOOL_CAST' :
        case 'T_BREAK' :
        case 'T_CASE' :
        case 'T_CATCH' :
        case 'T_CLASS' :
        case 'T_CONST' :
        case 'T_CONTINUE' :
          $h_php = str_replace($tok, '<b>' . $tok . '</b>', $h_php);
        break; 
        case 'T_AND_EQUAL' :
        case 'T_CONCAT_EQUAL' :
          $h_php = str_replace($tok, '<span style="color:#000080;">' . $tok . '</span>', $h_php);
        break;
        case 'T_BOOLEAN_AND' :
        case 'T_BOOLEAN_OR' :
          $h_php = str_replace($tok, '<span style="color:#ff0000;">' . $tok . '</span>', $h_php);
        break;
        case 'T_CONSTANT_ENCAPSED_STRING' :
          $h_php = str_replace($tok, '<span style="color:#0000ff;">' . $tok . '</span>', $h_php);
        break;
      }
    }
  }

  $h_php = str_replace('<?', '<?', $h_php);
  return $h_php;
}
$text = "<?php

//
// testkommentar
//
/* noamal */
/**
also
*/

continue
class
catch
case
break;
const

(array)
(bool)

&&||.=

foreach(\$tokens as \$token)

abstract
&=
function(\$test)
{
    \$code = \$test;
  
    echo \$code;
}

\$variable = array(\"testtext\" => 0);

?>";

echo "<pre><span style=\"font-family:'Courier New';font-size:10pt;\">" . nl2br(highlight_php($text)) . "</span></pre>";

?>