SPL-StandardPHPLibrary
regexiterator.inc
Go to the documentation of this file.
1 <?php
2 
21 {
22  const USE_KEY = 0x00000001;
25  const MATCH = 0;
26  const GET_MATCH = 1;
27  const ALL_MATCHES = 2;
28  const SPLIT = 3;
29  const REPLACE = 4;
31  private $regex;
32  private $mode;
34  private $flags;
35  private $preg_flags;
37  private $key;
38  private $current;
52  function __construct(Iterator $it, $regex, $mode = 0, $flags = 0, $preg_flags = 0) {
53  parent::__construct($it);
54  $this->regex = $regex;
55  $this->flags = $flags;
56  $this->mode = $mode;
57  $this->preg_flags = $preg_flags;
58  }
59 
68  function accept()
69  {
70  $matches = array();
71  $this->key = parent::key();
72  $this->current = parent::current();
73  /* note that we use $this->current, rather than calling parent::current() */
74  $subject = ($this->flags & self::USE_KEY) ? $this->key : $this->current;
75  switch($this->mode)
76  {
77  case self::MATCH:
78  return preg_match($this->regex, $subject, $matches, $this->preg_flags);
79 
80  case self::GET_MATCH:
81  $this->current = array();
82  return preg_match($this->regex, $subject, $this->current, $this->preg_flags) > 0;
83 
84  case self::ALL_MATCHES:
85  $this->current = array();
86  return preg_match_all($this->regex, $subject, $this->current, $this->preg_flags) > 0;
87 
88  case self::SPLIT:
89  $this->current = array();
90  preg_split($this->regex, $subject, $this->current, $this->preg_flags) > 1;
91 
92  case self::REPLACE:
93  $this->current = array();
94  $result = preg_replace($this->regex, $this->replacement, $subject);
95  if ($this->flags & self::USE_KEY)
96  {
97  $this->key = $result;
98  }
99  else
100  {
101  $this->current = $result;
102  }
103  }
104  }
105 
108  function key()
109  {
110  return $this->key;
111  }
112 
115  function current()
116  {
117  return $this->current;
118  }
119 
122  function getMode()
123  {
124  return $this->mode;
125  }
126 
129  function setMode($mode)
130  {
131  $this->mode = $mode;
132  }
133 
136  function getFlags()
137  {
138  return $this->flags;
139  }
140 
143  function setFlags($flags)
144  {
145  $this->flags = $flags;
146  }
147 
150  function getPregFlags()
151  {
152  return $this->preg_flags;
153  }
154 
158  {
159  $this->preg_flags = $preg_flags;
160  }
161 
164  function getRegex()
165  {
166  return $this->regex;
167  }
168 }
169 
170 ?>
$flags
special flags (self::USE_KEY)
accept()
Match current or key against regular expression using mode, flags and preg_flags. ...
const ALL_MATCHES
Mode: Return all matches (if any)
setPregFlags($preg_flags)
const USE_KEY
If present in $flags the key is used rather then the current value.
$regex
the regular expression to match against
const MATCH
Mode: Executed a plain match only.
Abstract filter for iterators.
const SPLIT
Mode: Return the split values (if any)
const GET_MATCH
Mode: Return the first matche (if any)
Regular expression filter for iterators.
$mode
operation mode (one of self::MATCH, self::GET_MATCH, self::ALL_MATCHES, self::SPLIT) ...
Basic iterator.
Definition: spl.php:549
const REPLACE
Mode: Replace the input key or current.
$preg_flags
PREG_* flags, see preg_match(), preg_match_all(), preg_split()
$key
the value used for key()
$current
the value used for current()
__construct(Iterator $it, $regex, $mode=0, $flags=0, $preg_flags=0)
Constructs a regular expression filter around an iterator whose elemnts or keys are strings...