PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/common/libraries/plugin/pear/phing/util/regexp/PregEngine.php

https://bitbucket.org/renaatdemuynck/chamilo
PHP | 183 lines | 68 code | 17 blank | 98 comment | 2 complexity | 10425f7cb1a863d800677f79ede0e461 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT, GPL-2.0
  1. <?php
  2. /*
  3. * $Id: PregEngine.php 782 2010-05-14 14:52:51Z mrook $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://phing.info>.
  20. */
  21. require_once 'phing/util/regexp/RegexpEngine.php';
  22. /**
  23. * PREG Regexp Engine.
  24. * Implements a regexp engine using PHP's preg_match(), preg_match_all(), and preg_replace() functions.
  25. *
  26. * @author hans lellelid, hans@velum.net
  27. * @package phing.util.regex
  28. */
  29. class PregEngine implements RegexpEngine
  30. {
  31. /**
  32. * Set to null by default to distinguish between false and not set
  33. * @var boolean
  34. */
  35. private $ignoreCase = null;
  36. /**
  37. * Set to null by default to distinguish between false and not set
  38. * @var boolean
  39. */
  40. private $multiline = null;
  41. /**
  42. * Pattern modifiers
  43. * @link http://php.net/manual/en/reference.pcre.pattern.modifiers.php
  44. * @var string
  45. */
  46. private $modifiers = null;
  47. /**
  48. * Sets pattern modifiers for regex engine
  49. *
  50. * @param string $mods Modifiers to be applied to a given regex
  51. * @return void
  52. */
  53. public function setModifiers($mods)
  54. {
  55. $this->modifiers = (string) $mods;
  56. }
  57. /**
  58. * Gets pattern modifiers.
  59. * @return string
  60. */
  61. public function getModifiers()
  62. {
  63. $mods = $this->modifiers;
  64. if ($this->getIgnoreCase())
  65. {
  66. $mods .= 'i';
  67. }
  68. elseif ($this->getIgnoreCase() === false)
  69. {
  70. $mods = str_replace('i', '', $mods);
  71. }
  72. if ($this->getMultiline())
  73. {
  74. $mods .= 's';
  75. }
  76. elseif ($this->getMultiline() === false)
  77. {
  78. $mods = str_replace('s', '', $mods);
  79. }
  80. // filter out duplicates
  81. $mods = preg_split('//', $mods, - 1, PREG_SPLIT_NO_EMPTY);
  82. $mods = implode('', array_unique($mods));
  83. return $mods;
  84. }
  85. /**
  86. * Sets whether or not regex operation is case sensitive.
  87. * @param boolean $bit
  88. * @return void
  89. */
  90. function setIgnoreCase($bit)
  91. {
  92. $this->ignoreCase = (boolean) $bit;
  93. }
  94. /**
  95. * Gets whether or not regex operation is case sensitive.
  96. * @return boolean
  97. */
  98. function getIgnoreCase()
  99. {
  100. return $this->ignoreCase;
  101. }
  102. /**
  103. * Sets whether regexp should be applied in multiline mode.
  104. * @param boolean $bit
  105. */
  106. function setMultiline($bit)
  107. {
  108. $this->multiline = $bit;
  109. }
  110. /**
  111. * Gets whether regexp is to be applied in multiline mode.
  112. * @return boolean
  113. */
  114. function getMultiline()
  115. {
  116. return $this->multiline;
  117. }
  118. /**
  119. * The pattern needs to be converted into PREG style -- which includes adding expression delims & any flags, etc.
  120. * @param string $pattern
  121. * @return string prepared pattern.
  122. */
  123. private function preparePattern($pattern)
  124. {
  125. // Use backquotes since hardly ever found in a regexp pattern, avoids using preg_quote
  126. return '`' . $pattern . '`' . $this->getModifiers();
  127. }
  128. /**
  129. * Matches pattern against source string and sets the matches array.
  130. * @param string $pattern The regex pattern to match.
  131. * @param string $source The source string.
  132. * @param array $matches The array in which to store matches.
  133. * @return boolean Success of matching operation.
  134. */
  135. function match($pattern, $source, &$matches)
  136. {
  137. return preg_match($this->preparePattern($pattern), $source, $matches);
  138. }
  139. /**
  140. * Matches all patterns in source string and sets the matches array.
  141. * @param string $pattern The regex pattern to match.
  142. * @param string $source The source string.
  143. * @param array $matches The array in which to store matches.
  144. * @return boolean Success of matching operation.
  145. */
  146. function matchAll($pattern, $source, &$matches)
  147. {
  148. return preg_match_all($this->preparePattern($pattern), $source, $matches);
  149. }
  150. /**
  151. * Replaces $pattern with $replace in $source string.
  152. * References to \1 group matches will be replaced with more preg-friendly
  153. * $1.
  154. * @param string $pattern The regex pattern to match.
  155. * @param string $replace The string with which to replace matches.
  156. * @param string $source The source string.
  157. * @return string The replaced source string.
  158. */
  159. function replace($pattern, $replace, $source)
  160. {
  161. // convert \1 -> $1, because we want to use the more generic \1 in the XML
  162. // but PREG prefers $1 syntax.
  163. $replace = preg_replace('/\\\(\d+)/', '\$$1', $replace);
  164. return preg_replace($this->preparePattern($pattern), $replace, $source);
  165. }
  166. }