RegexIterator Class Reference

Regular expression filter for iterators. More...

Inheritance diagram for RegexIterator:

Inheritance graph
{RecursiveRegexIterator\n|- $ref\l|+ __construct()\l+ getChildren()\l+ hasChildren()\l}
[legend]
List of all members.

Public Member Functions

 __construct (Iterator $it, $regex, $mode=0, $flags=0, $preg_flags=0)
 accept ()
 current ()
 getFlags ()
 getMode ()
 getPregFlags ()
 key ()
 setFlags ($flags)
 setMode ($mode)
 setPregFlags ($preg_flags)

Public Attributes

const ALL_MATCHES = 2
const GET_MATCH = 1
const MATCH = 0
const REPLACE = 4
const SPLIT = 3
const USE_KEY = 0x00000001

Private Attributes

 $current
 $flags
 $key
 $mode
 $preg_flags
 $regex

Detailed Description

Regular expression filter for iterators.

Author:
Marcus Boerger
Version:
1.0
Since:
PHP 5.1
This filter iterator assumes that the inner iterator

Definition at line 20 of file regexiterator.inc.


Constructor & Destructor Documentation

RegexIterator::__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.

Parameters:
it inner iterator
regex the regular expression to match
mode operation mode (one of self::MATCH, self::GET_MATCH, self::ALL_MATCHES, self::SPLIT)
flags special flags (self::USE_KEY)
preg_flags global PREG_* flags, see preg_match(), preg_match_all(), preg_split()

Definition at line 52 of file regexiterator.inc.

References $flags, $it, $mode, $preg_flags, and $regex.

00052                                                                                        {
00053         parent::__construct($it);
00054         $this->regex = $regex;
00055         $this->flags = $flags;
00056         $this->mode = $mode;
00057         $this->preg_flags = $preg_flags;
00058     }


Member Function Documentation

RegexIterator::accept (  ) 

Match current or key against regular expression using mode, flags and preg_flags.

Returns:
whether this is a match
Warning:
never call this twice for the same state

Definition at line 68 of file regexiterator.inc.

References current(), and key().

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     }

Here is the call graph for this function:

RegexIterator::currentRegexIterator::key

RegexIterator::current (  ) 

Returns:
the current value after accept has been called

Definition at line 115 of file regexiterator.inc.

Referenced by accept().

00116     {
00117         return $this->current;
00118     }

RegexIterator::getFlags (  ) 

Returns:
current operation flags

Definition at line 136 of file regexiterator.inc.

00137     {
00138         return $this->flags;
00139     }

RegexIterator::getMode (  ) 

Returns:
current operation mode

Definition at line 122 of file regexiterator.inc.

00123     {
00124         return $this->mode;
00125     }

RegexIterator::getPregFlags (  ) 

Returns:
current PREG flags

Definition at line 150 of file regexiterator.inc.

00151     {
00152         return $this->preg_flags;
00153     }

RegexIterator::key (  ) 

Returns:
the key after accept has been called

Definition at line 108 of file regexiterator.inc.

Referenced by accept().

00109     {
00110         return $this->key;
00111     }

RegexIterator::setFlags ( flags  ) 

Parameters:
flags new operaion flags

Definition at line 143 of file regexiterator.inc.

References $flags.

00144     {
00145         $this->flags = $flags;
00146     }

RegexIterator::setMode ( mode  ) 

Parameters:
mode new operaion mode

Definition at line 129 of file regexiterator.inc.

References $mode.

00130     {
00131         $this->mode = $mode;
00132     }

RegexIterator::setPregFlags ( preg_flags  ) 

Parameters:
preg_flags new PREG flags

Definition at line 157 of file regexiterator.inc.

References $preg_flags.

00158     {
00159         $this->preg_flags = $preg_flags;
00160     }


Member Data Documentation

RegexIterator::$current [private]

the value used for current()

Definition at line 38 of file regexiterator.inc.

RegexIterator::$flags [private]

special flags (self::USE_KEY)

Definition at line 34 of file regexiterator.inc.

Referenced by __construct(), RecursiveRegexIterator::__construct(), and setFlags().

RegexIterator::$key [private]

the value used for key()

Definition at line 37 of file regexiterator.inc.

RegexIterator::$mode [private]

operation mode (one of self::MATCH, self::GET_MATCH, self::ALL_MATCHES, self::SPLIT)

Definition at line 32 of file regexiterator.inc.

Referenced by __construct(), RecursiveRegexIterator::__construct(), and setMode().

RegexIterator::$preg_flags [private]

PREG_* flags, see preg_match(), preg_match_all(), preg_split().

Definition at line 35 of file regexiterator.inc.

Referenced by __construct(), RecursiveRegexIterator::__construct(), and setPregFlags().

RegexIterator::$regex [private]

the regular expression to match against

Definition at line 31 of file regexiterator.inc.

Referenced by __construct(), and RecursiveRegexIterator::__construct().

const RegexIterator::ALL_MATCHES = 2

Mode: Return all matches (if any).

Definition at line 27 of file regexiterator.inc.

const RegexIterator::GET_MATCH = 1

Mode: Return the first matche (if any).

Definition at line 26 of file regexiterator.inc.

const RegexIterator::MATCH = 0

Mode: Executed a plain match only.

Definition at line 25 of file regexiterator.inc.

const RegexIterator::REPLACE = 4

Mode: Replace the input key or current.

Definition at line 29 of file regexiterator.inc.

const RegexIterator::SPLIT = 3

Mode: Return the split values (if any).

Definition at line 28 of file regexiterator.inc.

const RegexIterator::USE_KEY = 0x00000001

If present in $flags the the key is used rather then the current value.

Definition at line 22 of file regexiterator.inc.


The documentation for this class was generated from the following file:
Generated on Thu Apr 26 01:07:22 2007 for SPL-StandardPHPLibrary by  doxygen 1.5.2