You are here

protected function RegexCounter::explodeAlternation in Bibliography Module 7.2

Explodes an alternation on outer pipes. NOTE: the given $pattern is assumed to not contain escaped parentheses nor escaped pipes.

Parameters

string $pattern a string with balanced (possibly nested) parentheses and pipes:

Return value

array

Throws

RegexCounterException

1 call to RegexCounter::explodeAlternation()
RegexCounter::countGroups in lib/msrc-authortool/src/Nametools/RegexCounter.php
Returns how many groups (numbered or named) there are in the given $pattern

File

lib/msrc-authortool/src/Nametools/RegexCounter.php, line 174

Class

RegexCounter
Regexcounter is a class that counts the expected number of matches for a given REGEX pattern

Namespace

Nametools

Code

protected function explodeAlternation($pattern) {
  $result = array();
  $token = '|';
  $open = 0;
  $start = 0;
  for ($i = $start, $iTop = strlen($pattern); $i < $iTop; $i++) {

    //$current = $pattern[$i];
    if ($pattern[$i] == '(') {
      $open += 1;
    }
    elseif ($pattern[$i] == ')') {
      $open -= 1;
    }
    elseif ($pattern[$i] == '|') {
      if (0 == $open) {
        $result[$start] = substr($pattern, $start, $i - $start);
        $start = $i + 1;
      }
    }
  }
  $result[$start] = substr($pattern, $start);
  if (0 != $open) {
    throw new RegexCounterException('Unbalanced parentheses.');
  }
  return $result;
}