Created
August 17, 2016 12:37
-
-
Save willemwollebrants/803be2f21406766562b72ad42b83e6d7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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