static function backrefs() { $r = preg_merge('', array('/(a)/', '/(b)\\1/')); Test::assert($r, 'preg_merge'); Test::assertNoMatch($r, 'aba'); Test::assertMatch($r, 'abb'); Test::assertNoMatch($r, 'ab'); Test::assertNoMatch($r, 'xbb'); $r = preg_merge('', array('/(a)/', '/(b)\\\\1/')); Test::assert($r, 'preg_merge'); Test::assertMatch($r, 'ab\\1'); $r = preg_merge('', array('/(a)/', '/(b)\\\\\\1/')); var_dump('/(b)\\\\\\1/'); var_dump($r); Test::assert($r, 'preg_merge'); Test::assertMatch($r, 'ab\\b'); $r = preg_merge('', array('/(a)/', '/(b)\\\\\\1/', '/c((d)\\2)/')); Test::assert($r, 'preg_merge'); Test::assertMatch($r, 'ab\\bcdd'); }
private function compileRules($rules) { $tmp = array(); // Preprocess keyword list and flatten nested lists: // End of regular expression matching keywords. $end = $this->_caseInsensitive ? ')\\b/i' : ')\\b/'; foreach ($rules as $name => $rule) { if (is_array($rule)) { if (self::isAssocArray($rule)) { // Array is a nested list of rules. foreach ($rule as $key => $value) { if (is_array($value)) { // Array represents a list of keywords. $value = '/\\b(?:' . implode('|', $value) . $end; } if (!is_string($key) or strlen($key) === 0) { $tmp[$name] = $value; } else { $tmp["{$name} {$key}"] = $value; } } } else { // Array represents a list of keywords. $rule = '/\\b(?:' . implode('|', $rule) . $end; $tmp[$name] = $rule; } } else { $tmp[$name] = $rule; } // if (is_array($rule)) } // foreach $ret = array(); foreach ($this->_states as $name => $state) { $regex_rules = array(); $regex_names = array(); $nesting_rules = array(); foreach ($state as $rule_name) { $rule = $tmp[$rule_name]; if ($rule instanceof Rule) { $nesting_rules[$rule_name] = $rule; } else { $regex_rules[] = $rule; $regex_names[] = $rule_name; } } $ret[$name] = array_merge(array(preg_merge('|', $regex_rules, $regex_names)), $nesting_rules); } return $ret; }