regexiterator.inc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /** @file regexiterator.inc
  3. * @ingroup SPL
  4. * @brief class RegexIterator
  5. * @author Marcus Boerger
  6. * @date 2003 - 2009
  7. *
  8. * SPL - Standard PHP Library
  9. */
  10. /**
  11. * @brief Regular expression filter for iterators
  12. * @author Marcus Boerger
  13. * @version 1.0
  14. * @since PHP 5.1
  15. *
  16. * This filter iterator assumes that the inner iterator
  17. */
  18. class RegexIterator extends FilterIterator
  19. {
  20. const USE_KEY = 0x00000001; /**< If present in $flags the key is
  21. used rather then the current value. */
  22. const MATCH = 0; /**< Mode: Executed a plain match only */
  23. const GET_MATCH = 1; /**< Mode: Return the first matche (if any) */
  24. const ALL_MATCHES = 2; /**< Mode: Return all matches (if any) */
  25. const SPLIT = 3; /**< Mode: Return the split values (if any) */
  26. const REPLACE = 4; /**< Mode: Replace the input key or current */
  27. private $regex; /**< the regular expression to match against */
  28. private $mode; /**< operation mode (one of self::MATCH,
  29. self::GET_MATCH, self::ALL_MATCHES, self::SPLIT) */
  30. private $flags; /**< special flags (self::USE_KEY) */
  31. private $preg_flags;/**< PREG_* flags, see preg_match(), preg_match_all(),
  32. preg_split() */
  33. private $key; /**< the value used for key() */
  34. private $current; /**< the value used for current() */
  35. /**
  36. * Constructs a regular expression filter around an iterator whose
  37. * elemnts or keys are strings.
  38. *
  39. * @param it inner iterator
  40. * @param regex the regular expression to match
  41. * @param mode operation mode (one of self::MATCH, self::GET_MATCH,
  42. * self::ALL_MATCHES, self::SPLIT)
  43. * @param flags special flags (self::USE_KEY)
  44. * @param preg_flags global PREG_* flags, see preg_match(),
  45. * preg_match_all(), preg_split()
  46. */
  47. function __construct(Iterator $it, $regex, $mode = 0, $flags = 0, $preg_flags = 0) {
  48. parent::__construct($it);
  49. $this->regex = $regex;
  50. $this->flags = $flags;
  51. $this->mode = $mode;
  52. $this->preg_flags = $preg_flags;
  53. }
  54. /**
  55. * Match current or key against regular expression using mode, flags and
  56. * preg_flags.
  57. *
  58. * @return whether this is a match
  59. *
  60. * @warning never call this twice for the same state
  61. */
  62. function accept()
  63. {
  64. $matches = array();
  65. $this->key = parent::key();
  66. $this->current = parent::current();
  67. /* note that we use $this->current, rather than calling parent::current() */
  68. $subject = ($this->flags & self::USE_KEY) ? $this->key : $this->current;
  69. switch($this->mode)
  70. {
  71. case self::MATCH:
  72. return preg_match($this->regex, $subject, $matches, $this->preg_flags);
  73. case self::GET_MATCH:
  74. $this->current = array();
  75. return preg_match($this->regex, $subject, $this->current, $this->preg_flags) > 0;
  76. case self::ALL_MATCHES:
  77. $this->current = array();
  78. return preg_match_all($this->regex, $subject, $this->current, $this->preg_flags) > 0;
  79. case self::SPLIT:
  80. $this->current = array();
  81. preg_split($this->regex, $subject, $this->current, $this->preg_flags) > 1;
  82. case self::REPLACE:
  83. $this->current = array();
  84. $result = preg_replace($this->regex, $this->replacement, $subject);
  85. if ($this->flags & self::USE_KEY)
  86. {
  87. $this->key = $result;
  88. }
  89. else
  90. {
  91. $this->current = $result;
  92. }
  93. }
  94. }
  95. /** @return the key after accept has been called
  96. */
  97. function key()
  98. {
  99. return $this->key;
  100. }
  101. /** @return the current value after accept has been called
  102. */
  103. function current()
  104. {
  105. return $this->current;
  106. }
  107. /** @return current operation mode
  108. */
  109. function getMode()
  110. {
  111. return $this->mode;
  112. }
  113. /** @param mode new operaion mode
  114. */
  115. function setMode($mode)
  116. {
  117. $this->mode = $mode;
  118. }
  119. /** @return current operation flags
  120. */
  121. function getFlags()
  122. {
  123. return $this->flags;
  124. }
  125. /** @param flags new operaion flags
  126. */
  127. function setFlags($flags)
  128. {
  129. $this->flags = $flags;
  130. }
  131. /** @return current PREG flags
  132. */
  133. function getPregFlags()
  134. {
  135. return $this->preg_flags;
  136. }
  137. /** @param preg_flags new PREG flags
  138. */
  139. function setPregFlags($preg_flags)
  140. {
  141. $this->preg_flags = $preg_flags;
  142. }
  143. /** @return current regular expression
  144. */
  145. function getRegex()
  146. {
  147. return $this->regex;
  148. }
  149. }
  150. ?>