You are here

function codefilter_process_php in Code Filter 5

Same name and namespace in other branches
  1. 6 codefilter.module \codefilter_process_php()
  2. 7 codefilter.module \codefilter_process_php()

Processes chunks of escaped PHP code into HTML.

File

./codefilter.module, line 28

Code

function codefilter_process_php($text) {

  // Note, pay attention to odd preg_replace-with-/e behaviour on slashes
  // Undo possible linebreak filter conversion
  $text = preg_replace('@</?(br|p)\\s*/?>@', '', str_replace('\\"', '"', $text));

  // Undo the escaping in the prepare step
  $text = decode_entities($text);

  // Trim leading and trailing linebreaks
  $text = trim($text, "\r\n");

  // Highlight as PHP
  $text = '<div class="codeblock">' . highlight_string("<?php\n{$text}\n?>", 1) . '</div>';

  // In PHP4, highlight_string() returns font tags; replace them with span tags.
  $text = str_replace(array(
    '<font color="',
    '</font>',
  ), array(
    '<span style="color: ',
    '</span>',
  ), $text);

  // Remove newlines to avoid clashing with the linebreak filter
  $text = str_replace("\n", '', $text);
  return codefilter_fix_spaces($text);
}