|
SPL-StandardPHPLibrary
|
00001 <?php 00002 00020 class RegexIterator extends FilterIterator 00021 { 00022 const USE_KEY = 0x00000001; 00025 const MATCH = 0; 00026 const GET_MATCH = 1; 00027 const ALL_MATCHES = 2; 00028 const SPLIT = 3; 00029 const REPLACE = 4; 00031 private $regex; 00032 private $mode; 00034 private $flags; 00035 private $preg_flags; 00037 private $key; 00038 private $current; 00052 function __construct(Iterator $it, $regex, $mode = 0, $flags = 0, $preg_flags = 0) { 00053 parent::__construct($it); 00054 $this->regex = $regex; 00055 $this->flags = $flags; 00056 $this->mode = $mode; 00057 $this->preg_flags = $preg_flags; 00058 } 00059 00068 function accept() 00069 { 00070 $matches = array(); 00071 $this->key = parent::key(); 00072 $this->current = parent::current(); 00073 /* note that we use $this->current, rather than calling parent::current() */ 00074 $subject = ($this->flags & self::USE_KEY) ? $this->key : $this->current; 00075 switch($this->mode) 00076 { 00077 case self::MATCH: 00078 return preg_match($this->regex, $subject, $matches, $this->preg_flags); 00079 00080 case self::GET_MATCH: 00081 $this->current = array(); 00082 return preg_match($this->regex, $subject, $this->current, $this->preg_flags) > 0; 00083 00084 case self::ALL_MATCHES: 00085 $this->current = array(); 00086 return preg_match_all($this->regex, $subject, $this->current, $this->preg_flags) > 0; 00087 00088 case self::SPLIT: 00089 $this->current = array(); 00090 preg_split($this->regex, $subject, $this->current, $this->preg_flags) > 1; 00091 00092 case self::REPLACE: 00093 $this->current = array(); 00094 $result = preg_replace($this->regex, $this->replacement, $subject); 00095 if ($this->flags & self::USE_KEY) 00096 { 00097 $this->key = $result; 00098 } 00099 else 00100 { 00101 $this->current = $result; 00102 } 00103 } 00104 } 00105 00108 function key() 00109 { 00110 return $this->key; 00111 } 00112 00115 function current() 00116 { 00117 return $this->current; 00118 } 00119 00122 function getMode() 00123 { 00124 return $this->mode; 00125 } 00126 00129 function setMode($mode) 00130 { 00131 $this->mode = $mode; 00132 } 00133 00136 function getFlags() 00137 { 00138 return $this->flags; 00139 } 00140 00143 function setFlags($flags) 00144 { 00145 $this->flags = $flags; 00146 } 00147 00150 function getPregFlags() 00151 { 00152 return $this->preg_flags; 00153 } 00154 00157 function setPregFlags($preg_flags) 00158 { 00159 $this->preg_flags = $preg_flags; 00160 } 00161 00164 function getRegex() 00165 { 00166 return $this->regex; 00167 } 00168 } 00169 00170 ?>
1.7.5.1