1: <?php
2: class QRegex extends QBaseClass {
3:
4: const RegexBookend = "/";
5:
6: protected $objPatterns = array();
7: protected $objLabels = array();
8:
9: private $strRegex;
10:
11: public function addPattern($strPattern,$strLabel = true) {
12: array_push($this->objPatterns, "(" . $this->scrubPattern($strPattern) . ")");
13: array_push($this->objLabels, $strLabel);
14: $this->strRegex = NULL;
15: }
16:
17: protected function scrubPattern($strPattern) {
18: return str_replace(
19: array('/', '(', ')', '<', '>'),
20: array('\/', '\(', '\)', '\<', '\>'),
21: $strPattern);
22: }
23:
24: public function match($strSubject,&$strMatch) {
25: if(sizeof($this->objPatterns) == 0)
26: return FALSE;
27:
28: $this->compileRegex();
29:
30: if(!preg_match($this->strRegex,$strSubject,$objMatches)) {
31: $strMatch = "";
32: return FALSE;
33: }
34:
35: $strMatch = $objMatches[0];
36:
37: for ($i = 1; $i < count($objMatches); $i++) {
38: if ($objMatches[$i]) {
39: return $this->objLabels[$i - 1];
40: }
41: }
42:
43: return TRUE;
44: }
45:
46: private function compileRegex() {
47: if(!is_null($this->strRegex))
48: return;
49:
50: $this->strRegex = QRegex::RegexBookend . implode("|",$this->objPatterns) . QRegex::RegexBookend . "msSi";
51: }
52: }