SleePyCode Forums

Special Site & Other Boards => PasteBin => Topic started by: Guest on Oct 13, 2009, 11:45 PM

Title: Paste-1255477513:v:use_geshi-1:v:type-php
Post by: Guest on Oct 13, 2009, 11:45 PM
// Highlight any code...
function highlight_php_code($code)
{
   global $context;

   // Remove special characters.
   $code = un_htmlspecialchars(strtr($code, array('
', "\t", '['), array("\n", 'SMF_TAB();', '['), $code));

   // It's easier in 4.2.x+.
   if (version_compare(PHP_VERSION, '4.2.0') == -1)
   {
      ob_start();
      highlight_string($code);
      $buffer = str_replace(array("\n", "\r"), '', ob_get_contents());
      ob_end_clean();
   }
   else
      $buffer = str_replace(array("\n", "\r"), '', highlight_string($code, true));

   // Yes, I know this is kludging it, but this is the best way to preserve tabs from PHP :P.
   $buffer = preg_replace('~SMF_TAB(?:</(?:font|span)><(?:font color|span style)="[^"]*?">)?\\(\\);~', '<pre style="display: inline;">' . "\t" . '', $buffer);

   return str_replace(array('\'', '<code>', '</code>'), array('&#039;', '', ''), $buffer);
}
Title: Paste-1255477585:v:use_geshi-1:v:type-php
Post by: adfadsfadsf on Oct 13, 2009, 11:46 PM
// Highlight any code...
function highlight_php_code($code)
{
   global $context;

   // Remove special characters.
   $code = un_htmlspecialchars(str_replace(array('
', "\t", '&#91;'), array("\n", 'SMF_TAB();', '['), $code));

   // It's easier in 4.2.x+.
   if (version_compare(PHP_VERSION, '4.2.0') == -1)
   {
      ob_start();
      highlight_string($code);
      $buffer = str_replace(array("\n", "\r"), '', ob_get_contents());
      ob_end_clean();
   }
   else
      $buffer = str_replace(array("\n", "\r"), '', highlight_string($code, true));

   // Yes, I know this is kludging it, but this is the best way to preserve tabs from PHP :P.
   $buffer = preg_replace('~SMF_TAB(?:</(?:font|span)><(?:font color|span style)="[^"]*?">)?\\(\\);~', '<pre style="display: inline;">' . "\t" . '', $buffer);

   return str_replace(array('\'', '<code>', '</code>'), array('&#039;', '', ''), $buffer);
}