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

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

https://bitbucket.org/chamilo/chamilo/
PHP | 167 lines | 52 code | 17 blank | 98 comment | 2 complexity | bd7c8db4a75ef9bed70a359d4d05b3d6 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  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. * Set to null by default to distinguish between false and not set
  32. * @var boolean
  33. */
  34. private $ignoreCase = null;
  35. /**
  36. * Set to null by default to distinguish between false and not set
  37. * @var boolean
  38. */
  39. private $multiline = null;
  40. /**
  41. * Pattern modifiers
  42. * @link http://php.net/manual/en/reference.pcre.pattern.modifiers.php
  43. * @var string
  44. */
  45. private $modifiers = null;
  46. /**
  47. * Sets pattern modifiers for regex engine
  48. *
  49. * @param string $mods Modifiers to be applied to a given regex
  50. * @return void
  51. */
  52. public function setModifiers($mods) {
  53. $this->modifiers = (string)$mods;
  54. }
  55. /**
  56. * Gets pattern modifiers.
  57. * @return string
  58. */
  59. public function getModifiers() {
  60. $mods = $this->modifiers;
  61. if($this->getIgnoreCase()) {
  62. $mods .= 'i';
  63. } elseif($this->getIgnoreCase() === false) {
  64. $mods = str_replace('i', '', $mods);
  65. }
  66. if($this->getMultiline()) {
  67. $mods .= 's';
  68. } elseif($this->getMultiline() === false) {
  69. $mods = str_replace('s', '', $mods);
  70. }
  71. // filter out duplicates
  72. $mods = preg_split('//', $mods, -1, PREG_SPLIT_NO_EMPTY);
  73. $mods = implode('', array_unique($mods));
  74. return $mods;
  75. }
  76. /**
  77. * Sets whether or not regex operation is case sensitive.
  78. * @param boolean $bit
  79. * @return void
  80. */
  81. function setIgnoreCase($bit) {
  82. $this->ignoreCase = (boolean) $bit;
  83. }
  84. /**
  85. * Gets whether or not regex operation is case sensitive.
  86. * @return boolean
  87. */
  88. function getIgnoreCase() {
  89. return $this->ignoreCase;
  90. }
  91. /**
  92. * Sets whether regexp should be applied in multiline mode.
  93. * @param boolean $bit
  94. */
  95. function setMultiline($bit) {
  96. $this->multiline = $bit;
  97. }
  98. /**
  99. * Gets whether regexp is to be applied in multiline mode.
  100. * @return boolean
  101. */
  102. function getMultiline() {
  103. return $this->multiline;
  104. }
  105. /**
  106. * The pattern needs to be converted into PREG style -- which includes adding expression delims & any flags, etc.
  107. * @param string $pattern
  108. * @return string prepared pattern.
  109. */
  110. private function preparePattern($pattern)
  111. {
  112. // Use backquotes since hardly ever found in a regexp pattern, avoids using preg_quote
  113. return '`'.$pattern.'`' . $this->getModifiers();
  114. }
  115. /**
  116. * Matches pattern against source string and sets the matches array.
  117. * @param string $pattern The regex pattern to match.
  118. * @param string $source The source string.
  119. * @param array $matches The array in which to store matches.
  120. * @return boolean Success of matching operation.
  121. */
  122. function match($pattern, $source, &$matches) {
  123. return preg_match($this->preparePattern($pattern), $source, $matches);
  124. }
  125. /**
  126. * Matches all patterns in source string and sets the matches array.
  127. * @param string $pattern The regex pattern to match.
  128. * @param string $source The source string.
  129. * @param array $matches The array in which to store matches.
  130. * @return boolean Success of matching operation.
  131. */
  132. function matchAll($pattern, $source, &$matches) {
  133. return preg_match_all($this->preparePattern($pattern), $source, $matches);
  134. }
  135. /**
  136. * Replaces $pattern with $replace in $source string.
  137. * References to \1 group matches will be replaced with more preg-friendly
  138. * $1.
  139. * @param string $pattern The regex pattern to match.
  140. * @param string $replace The string with which to replace matches.
  141. * @param string $source The source string.
  142. * @return string The replaced source string.
  143. */
  144. function replace($pattern, $replace, $source) {
  145. // convert \1 -> $1, because we want to use the more generic \1 in the XML
  146. // but PREG prefers $1 syntax.
  147. $replace = preg_replace('/\\\(\d+)/', '\$$1', $replace);
  148. return preg_replace($this->preparePattern($pattern), $replace, $source);
  149. }
  150. }