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().

$iterator = pattern('\w+')->match('Apples are cool')->getIterator();
return $iterator->current()->text(); // Iterator contains "Detail"

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.

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