Skip to content

Instantly share code, notes, and snippets.

@willemwollebrants
Created August 17, 2016 12:37
Show Gist options
  • Save willemwollebrants/803be2f21406766562b72ad42b83e6d7 to your computer and use it in GitHub Desktop.
Save willemwollebrants/803be2f21406766562b72ad42b83e6d7 to your computer and use it in GitHub Desktop.
<?php
namespace Studiow\Util;
class MatchAll
{
private $result;
private $matches = [];
public function __construct($pattern, $subject, $flags = PREG_PATTERN_ORDER, $offset = 0)
{
$this->result = (bool)preg_match_all($pattern, $subject, $this->matches, $flags, $offset);
}
public function getMatches($group_num = null)
{
if (is_null($group_num)) {
return $this->matches;
} else {
$number = intval($group_num);
if (!array_key_exists($number, $this->matches)) {
throw new \OutOfRangeException("Invalid group number {$number}");
}
return $this->matches[$number];
}
}
public function result()
{
return $this->result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment