You are here

function coder_replace_multiple_vars in Coder 6.2

Same name and namespace in other branches
  1. 5.2 scripts/coder_format/coder_format.inc \coder_replace_multiple_vars()
  2. 5 scripts/coder_format/coder_format.inc \coder_replace_multiple_vars()
  3. 6 scripts/coder_format/coder_format.inc \coder_replace_multiple_vars()

Related topics

1 string reference to 'coder_replace_multiple_vars'
coder_postprocessor_multiple_vars in scripts/coder_format/coder_format.inc

File

scripts/coder_format/coder_format.inc, line 1246
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;
}