3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Element { const TYPE_LITERAL = 0; const TYPE_GROUP = 1; const END_OF_STRING = 2; public $type; public $value; public $next = self::END_OF_STRING; } function buildGraph($tokens) { $root = new Element; $root->type = Element::TYPE_GROUP; while ( $tokens->valid() ) { $token = $tokens->current(); $tokens->next(); switch ($token) { case '(': if ( isset($current) ) { $current->next = buildGraph($tokens); $current = $current->next; } else { $root->value[] = buildGraph($tokens); $current = end($root->value); } break; case ')': $current->next = &$root->next; return $root; case '|': $current->next = &$root->next; unset($current); break; default: if ( isset($current) ) { $current->next = new Element; $current = $current->next; } else { $root->value[] = new Element; $current = end($root->value); } $current->type = Element::TYPE_LITERAL; $current->value = $token; } } $root->next = Element::END_OF_STRING; return $root; } function getPaths($elt, $prefix = '') { if ( $elt === Element::END_OF_STRING ) { yield $prefix; return; } switch($elt->type) { case Element::TYPE_LITERAL: $prefix .= $elt->value; yield from getPaths($elt->next, $prefix); break; case Element::TYPE_GROUP: foreach ($elt->value as $value) { yield from getPaths($value, $prefix); } break; } } $pattern = 'chap(i|a) chap(o|a) p(a|i)t(a|i)p(o|a)'; // validation $validationPattern = '~ \A ( [^()]+ | \( (?1)* \) )* \z ~x'; if ( !preg_match($validationPattern, $pattern) ) throw new Exception('invalid pattern.'); // tokenization $tokens = (function ($regex) { if ( preg_match_all('~ [^()|]+ | (?<![^(|]) (?![^|)]) | [()|] ~x', $regex, $matches) ) yield from $matches[0]; })($pattern); $graph = buildGraph($tokens); foreach (getPaths($graph) as $path) { echo $path, PHP_EOL; }
Output for git.master, git.master_jit, rfc.property-hooks
chapi chapo patapo chapi chapo patapa chapi chapo patipo chapi chapo patipa chapi chapo pitapo chapi chapo pitapa chapi chapo pitipo chapi chapo pitipa chapi chapa patapo chapi chapa patapa chapi chapa patipo chapi chapa patipa chapi chapa pitapo chapi chapa pitapa chapi chapa pitipo chapi chapa pitipa chapa chapo patapo chapa chapo patapa chapa chapo patipo chapa chapo patipa chapa chapo pitapo chapa chapo pitapa chapa chapo pitipo chapa chapo pitipa chapa chapa patapo chapa chapa patapa chapa chapa patipo chapa chapa patipa chapa chapa pitapo chapa chapa pitapa chapa chapa pitipo chapa chapa pitipa

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
164.09 ms | 407 KiB | 5 Q