You are here

private static function TableTagPlugin::tabulateText in Extensible BBCode 8.3

Same name and namespace in other branches
  1. 4.0.x standard/src/Plugin/XBBCode/TableTagPlugin.php \Drupal\xbbcode_standard\Plugin\XBBCode\TableTagPlugin::tabulateText()

Tabulate a text into lines and columns.

Parameters

string $text: The text to tabulate.

Return value

string[][] The tabulated array, or false if it is atomic.

1 call to TableTagPlugin::tabulateText()
TableTagPlugin::tabulateTree in standard/src/Plugin/XBBCode/TableTagPlugin.php
Helper that turns a parse tree into a table of cells.

File

standard/src/Plugin/XBBCode/TableTagPlugin.php, line 132

Class

TableTagPlugin
Renders a table.

Namespace

Drupal\xbbcode_standard\Plugin\XBBCode

Code

private static function tabulateText($text) : array {

  // Trim, and strip linebreaks before newlines.
  $trimmed = preg_replace('/<br\\s*\\/?>\\n/', "\n", $text);
  $breaks = $trimmed !== $text;
  $text = trim($trimmed);

  // Tokenize on linebreaks and commas. Collapse multiple linebreaks.
  preg_match_all("/\n      (?:\n        (?'quote'['\"]|&quot;|&\\#039;)\n        (?'quoted'\n          (?:\\\\.|(?!\\\\|\\k'quote')[^\\\\])*\n        )\n        \\k'quote'\n        |\n        (?'unquoted'\n          (?:\\\\.|[^\\\\,\\v])*\n        )\n      )\n      (?'delimiter',|\\v+|\$)\n      /x", $text, $match, PREG_SET_ORDER);
  array_pop($match);
  $rows = [];
  $row = [];
  foreach ((array) $match as $token) {
    $value = stripslashes($token['quoted'] ?: $token['unquoted']);

    // Reinsert HTML linebreaks, if we removed them.
    if ($breaks) {
      $value = nl2br($value);
    }
    $row[] = $value;

    // Unless it is a column delimiter, end the row.
    if ($token['delimiter'] !== ',') {
      $rows[] = $row;
      $row = [];
    }
  }
  return $rows;
}