Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Wyrazenia regularne w OOP
Forum PHP.pl > Inne > Oceny
q.michal
Witam,

Tym razem przedstawiam klase do obslugi wyrazen regularnych.
Jak zwykle czekam na Wasze opinie i pozdrawiam wszystkich forumowiczow wink.gif

  1. <?php
  2.  
  3. final class Regexp {
  4. private $matches;
  5. private $regexp;
  6.  
  7. /**
  8. * Regexp class constructor
  9. *
  10. * @param string regular expression without modifiers
  11. * @param integer modifiers to the regular expression
  12. * @return object Regexp object
  13. */
  14. public function __construct($regexp, $modifiers) {
  15. $regexp = str_replace('/', '\\/', $regexp);
  16. $this->regexp = '/' . $regexp . '/';
  17. if($modifiers & REGEXP_CASE_INSENSITIVE) $this->regexp .= 'i';
  18. if($modifiers & REGEXP_DOT_ALL) $this->regexp .= 's';
  19. if($modifiers & REGEXP_IGNORE_WHITESPACE) $this->regexp .= 'x';
  20. if($modifiers & REGEXP_MULTILINE) $this->regexp .= 'm';
  21. if(!($modifiers & REGEXP_NO_ANALYSE)) $this->regexp .= 'S';
  22. if($modifiers & REGEXP_UNGREEDY) $this->regexp .= 'U';
  23. if($modifiers & REGEXP_UTF8) $this->regexp .= 'u';
  24. }
  25.  
  26. /**
  27. * Checks whether the regular expression operation ended successfully or not
  28. *
  29. * @param mixed regular expression operation return value
  30. * @return mixed regular expression operation return value
  31. * @throws exception if regular expression operation ended with an error
  32. */
  33. private function checkResult($result) {
  34. $code = preg_last_error();
  35. if($code != PREG_NO_ERROR || $result === false || $result === NULL) {
  36. switch($code) {
  37. case PREG_BACKTRACE_LIMIT_ERROR:
  38. $message = 'Backtrack limit exhausted';
  39. break;
  40. case PREG_BAD_UTF8_ERROR:
  41. $message = 'Bad UTF-8 encoding';
  42. break;
  43. case PREG_INTERNAL_ERROR:
  44. $message = 'Internal REGEXP error';
  45. break;
  46. case PREG_RECURSION_LIMIT_ERROR:
  47. $message = 'Recursion limit exhausted';
  48. break;
  49. default:
  50. $message = 'Unknown REGEXP error';
  51. break;
  52. }
  53. $error = sprintf('Could not execute regular expression (%s): %s', $this->regexp, $message);
  54. throw new Throwable\Regexp($error);
  55. }
  56. return $result;
  57. }
  58.  
  59. /**
  60. * Performs a regular expression search and replace
  61. *
  62. * @param mixed string or an array with strings to replace
  63. * @param mixed string or an array with strings to search and replace
  64. * @return mixed array if $subject id an array, or string otherwise
  65. * @throws exception if regular expression operation ended with an error
  66. */
  67. public function filter($replacement, $subject) {
  68. return $this->checkResult(preg_filter($this->regexp, $replacement, $subject));
  69. }
  70.  
  71. /**
  72. * Returns the global Regexp object
  73. *
  74. * @param string regular expression without modifiers
  75. * @param integer modifiers to the regular expression
  76. * @return object Regexp object
  77. */
  78. public static function create($regexp, $modifiers = REGEXP_MODIFIER_NONE) {
  79. return new Regexp($regexp, $modifiers);
  80. }
  81.  
  82. /**
  83. * Returns the matches of the last regular expression operation
  84. *
  85. * @return array results of last regular expression operation
  86. */
  87. public function getMatches() {
  88. return $this->matches;
  89. }
  90.  
  91. /**
  92. * Returns the compiled regular expression
  93. *
  94. * @return string compiled regular expression
  95. */
  96. public function getRegexp() {
  97. return $this->regexp;
  98. }
  99.  
  100. /**
  101. * Returns an array of entries that match the pattern
  102. *
  103. * @param array input array
  104. * @param integer combined by the bitwise operator flags passed to preg_grep()
  105. * @return array array consisting of the elements that matches the regular expression
  106. * @throws exception if regular expression operation ended with an error
  107. */
  108. public function grep($subject, $flags = 0) {
  109. return $this->checkResult(preg_grep($this->regexp, $subject, $flags));
  110. }
  111.  
  112. /**
  113. * Checks whether the regular expression is syntactically correct
  114. *
  115. * @return boolean TRUE if regular expression has correct syntax, or FALSE otherwise
  116. */
  117. public function isValid() {
  118. try {
  119. $this->match();
  120. }
  121. catch(Throwable\Regexp $e) {
  122. return false;
  123. }
  124. return true;
  125. }
  126.  
  127. /**
  128. * Performs a regular expression match
  129. *
  130. * @param string the input string
  131. * @param boolean specifies whether to search for all occurences or just to find the first one
  132. * @param integer combined by the bitwise operator flags passed to preg_match()
  133. * @return integer the number of full pattern matches (which might be zero)
  134. * @throws exception if regular expression operation ended with an error
  135. */
  136. public function match($subject, $all = false, $flags = 0) {
  137. if($all) {
  138. return $this->checkResult(preg_match_all($this->regexp, $subject, $this->matches, $flags));
  139. }
  140. return $this->checkResult(preg_match($this->regexp, $subject, $this->matches, $flags));
  141. }
  142.  
  143. /**
  144. * Performs a regular expression search and replace
  145. *
  146. * @param mixed string or an array with strings to replace
  147. * @param mixed string or an array with strings to search and replace
  148. * @return mixed array if $subject id an array, or string otherwise
  149. * @throws exception if regular expression operation ended with an error
  150. */
  151. public function replace($replacement, $subject) {
  152. if(is_callable($replacement)) {
  153. return $this->checkResult(preg_replace_callback($this->regexp, $replacement, $subject));
  154. }
  155. return $this->checkResult(preg_replace($this->regexp, $replacement, $subject));
  156. }
  157.  
  158. /**
  159. * Splits a string by the regular expression
  160. *
  161. * @param string the input string
  162. * @param integer combined by the bitwise operator flags passed to preg_split()
  163. * @return array array containing substrings split along boundaries matched by regular expression
  164. * @throws exception if regular expression operation ended with an error
  165. */
  166. public function split($subject, $flags = 0) {
  167. return $this->checkResult(preg_split($this->regexp, $subject, NULL, $flags));
  168. }
  169.  
  170. } /* class */
  171.  
  172. ?>
Pyton_000
Nie ma sensu robienie singletona z tego bo w sumie i tak nim nie jest, a nazwa `getInstance` trochę myli.
Daj możliwość zrobienia normalnie obiektu z konstruktorem + ew. statyczny `create` które zwraca obiekt.


Nie masz $error w `switch($error) {`

Podczas błędów kompilacji patternu wywali Warning więc do valid powinieneś dodać @

Więcej nie ma co oceniać. Ot wrapper smile.gif
q.michal
Cytat(Pyton_000 @ 21.07.2017, 08:44:10 ) *
Nie ma sensu robienie singletona z tego bo w sumie i tak nim nie jest, a nazwa `getInstance` trochę myli.
Daj możliwość zrobienia normalnie obiektu z konstruktorem + ew. statyczny `create` które zwraca obiekt.

Racja, nazwa toche mylaca.

Cytat(Pyton_000 @ 21.07.2017, 08:44:10 ) *
Nie masz $error w `switch($error) {`


Powinno byc switch($code) {
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2018 Invision Power Services, Inc.