You are here

function _coder_replace_multiple_vars in Coder 7

Same name and namespace in other branches
  1. 7.2 scripts/coder_format/coder_format.inc \_coder_replace_multiple_vars()

Related topics

File

scripts/coder_format/coder_format.inc, line 1256
Coder format helper functions.

Code

function _coder_replace_multiple_vars($matches) {

  // Retrieve all variable name = variable value pairs.
  $regex = '@
    ^           # match start of a line
    (\\s*)       # match a single optional white-space char
    (\\$.+?)     # match a variable name
    \\ (.?)=\\    # match a variable assignment
    (.+?$)      # match a variable value including end of line
    @mx';
  preg_match_all($regex, $matches[0], $vars, PREG_SET_ORDER);

  // Determine the longest variable name.
  $maxlength = 0;
  foreach ($vars as $var) {
    if (strlen($var[2]) > $maxlength) {
      $maxlength = strlen($var[2] . $var[3]);
    }
  }

  // Realign variable values at the longest variable names.
  $return = '';
  $extra_spaces = 0;
  for ($c = 0, $cc = count($vars); $c < $cc; ++$c) {
    if ($maxlength <= 20) {
      $extra_spaces = $maxlength - strlen($vars[$c][2] . $vars[$c][3]);
    }
    $return .= $vars[$c][1] . $vars[$c][2];
    $return .= str_repeat(' ', $extra_spaces) . ' ' . $vars[$c][3] . '= ';
    $return .= $vars[$c][4];
    if ($c < $cc - 1) {

      // Append a line break, but not to the last variable assignment.
      $return .= "\n";
    }
  }
  return $return;
}