Skip to main content


T-Regx uses vanilla PHP \Iterator with standard methods:

  • current()
  • key(): scalar
  • next(): void
  • rewind(): void
  • valid(): bool

Iterator to array#

Method match()->getIterator() returns an implementation of PHP \Iterator which you can be used with other \Iterator methods or within your own code, for example PHP methods iterator_to_array() and iterator_count().

$matcher = pattern('\w+')->match('Apples are cool');
$iterator = $matcher->getIterator();
* @var Detail $detail
$detail = $iterator->current();
return $detail->text();

It is useful with methods that only accept \Iterator, and when an explicit iterator is needed. Using match()->getIterator() for methods that accept array is redundant :)

Also, please keep in mind that match() and other entities (such as match()->group(string|int) or match()->asInt()) are also iterable with foreach, making explicit usage of getIterator() even less likely.

$pattern = pattern('\w+');
* @var Detail $match
foreach ($pattern->match('Apples are cool') as $match) {
$text = $match->text();
Last updated on