You are here

public static function CodeFilter::processPHP in Code Filter 8

Processes chunks of escaped PHP code into HTML.

2 calls to CodeFilter::processPHP()
CodeFilter::processPHPCallback in src/Plugin/Filter/CodeFilter.php
Callback to replace content of the <?php ?> elements.
CodeFilterTest::testCodefilterProcessPHP in src/Tests/CodeFilterTest.php
Test codefilter_process_php().

File

src/Plugin/Filter/CodeFilter.php, line 188

Class

CodeFilter
Text filter for highlighting PHP source code.

Namespace

Drupal\codefilter\Plugin\Filter

Code

public static function processPHP($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 = Html::decodeEntities($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>';

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