Str.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. /**
  3. * Draft 0.2 for PHP argument order fix
  4. */
  5. /**
  6. * Fix/Unify order, unify _ (strstr to str_str etc).
  7. * @inspired by http://www.skyrocket.be/2009/05/30/php-function-naming-and-argument-order/comment-page-1
  8. *
  9. * The following functions use "needle hackstack":
  10. * - array_search
  11. * - in_array
  12. *
  13. * The following do it in reverse order and will be fixed with this class:
  14. * - strchr, stristr, strrchr, strstr
  15. * - strpos, strrpos, stripos, strripos, substr_count
  16. *
  17. * Also corrected is the naming of "rchr vs rrchr" by using "last" instead of "r":
  18. * - chr and lastChr
  19. * - pos, lastPos and iLastPos
  20. *
  21. */
  22. final class Str {
  23. /**
  24. * Avoid constructor conflicts.
  25. *
  26. * @return void
  27. */
  28. final public function __construct() {
  29. }
  30. /**
  31. * Find the first occurrence of a string.
  32. * Note: use iStr for CI
  33. *
  34. * @param mixed $needle
  35. * @param string $haystack
  36. * @param boolean $beforeNeedle (defaults to false)
  37. * @return mixed
  38. */
  39. final public static function str($needle, $haystack, $beforeNeedle = false) {
  40. return strstr($haystack, $needle, $beforeNeedle);
  41. }
  42. /**
  43. * Case-insensitive strstr().
  44. *
  45. * @param mixed $needle
  46. * @param string $haystack
  47. * @param boolean $beforeNeedle (defaults to false)
  48. * @return mixed
  49. */
  50. final public static function iStr($needle, $haystack, $beforeNeedle = false) {
  51. return stristr($haystack, $needle, $beforeNeedle);
  52. }
  53. /**
  54. * Find the first occurrence of a string - alias of strstr().
  55. *
  56. * @param mixed $needle
  57. * @param string $haystack
  58. * @param boolean $beforeNeedle (defaults to false)
  59. * @return mixed
  60. */
  61. final public static function chr($needle, $haystack, $beforeNeedle = false) {
  62. return strchr($haystack, $needle, $beforeNeedle);
  63. }
  64. /**
  65. * Find the last occurrence of a character in a string.
  66. * Note: If needle contains more than one character, only the first is used.
  67. * This behavior is different from that of strstr(). This behavior is different from that of strstr().
  68. * If needle is not a string, it is converted to an integer and applied as the ordinal value of a character.
  69. *
  70. * @param mixed $needle
  71. * @param string $haystack
  72. * @return mixed
  73. */
  74. final public static function lastChr($needle, $haystack) {
  75. return strrchr($haystack, $needle);
  76. }
  77. /**
  78. * Replace all occurrences of the search string with the replacement string.
  79. * Note: use iReplace for CI
  80. *
  81. * @param mixed $search
  82. * @param mixed $replace
  83. * @param mixed $subject
  84. * @param integer $count Reference to store count in
  85. * @return mixed
  86. */
  87. final public static function replace($search, $replace, $subject, &$count = null) {
  88. return str_replace($search, $replace, $subject, $count);
  89. }
  90. /**
  91. * Case-insensitive version of str_replace().
  92. *
  93. * @param mixed $search
  94. * @param mixed $replace
  95. * @param mixed $subject
  96. * @param integer $count Reference to store count in
  97. * @return mixed
  98. */
  99. final public static function iReplace($search, $replace, $subject, &$count = null) {
  100. return str_ireplace($search, $replace, $subject, $count);
  101. }
  102. /**
  103. * Replace text within a portion of a string.
  104. *
  105. * @param mixed $string
  106. * @param string $replacement
  107. * @return mixed
  108. */
  109. final public static function substrReplace($string, $replacement, $start, $length = null) {
  110. return substr_replace($string, $replacement, $start, $length);
  111. }
  112. /**
  113. * Count the number of substring occurrences.
  114. *
  115. * @param string $needle
  116. * @param string $haystack
  117. * @param integer $offset
  118. * @param integer $length
  119. * @return integer
  120. */
  121. final public static function count($needle, $haystack, $offset = 0, $length = null) {
  122. if ($length === null) {
  123. return substr_count($haystack, $needle, $offset);
  124. }
  125. return substr_count($haystack, $needle, $offset, $length);
  126. }
  127. /**
  128. * Binary safe comparison of two strings from an offset, up to length characters.
  129. * Note: use iCompare for CI (for the sake of consistency and less arguments - already enough)
  130. *
  131. * @param string $mainStr
  132. * @param string $str
  133. * @param integer $offset
  134. * @param integer $length
  135. * @return mixed
  136. */
  137. final public static function compare($mainStr, $str, $offset = 0, $length = null) {
  138. return substr_compare($mainStr, $str, $offset, $length);
  139. }
  140. /**
  141. * Binary safe comparison of two strings from an offset, up to length characters.
  142. *
  143. * @param string $mainStr
  144. * @param string $str
  145. * @param integer $offset
  146. * @param integer $length
  147. * @return mixed
  148. */
  149. final public static function iCompare($mainStr, $str, $offset = 0, $length = null) {
  150. return substr_compare($mainStr, $str, $offset, $length, true);
  151. }
  152. /**
  153. * Find the position of the first occurrence of a substring in a string.
  154. * Note: use iPos for CI (for the sake of consistency and less arguments - already enough)
  155. *
  156. * @param string $needle
  157. * @param string $haystack
  158. * @param integer $offset
  159. * @return mixed
  160. */
  161. final public static function pos($needle, $haystack, $offset = 0) {
  162. return strpos($haystack, $needle, $offset);
  163. }
  164. /**
  165. * Case-insensitive version of stripos().
  166. *
  167. * @param string $needle
  168. * @param string $haystack
  169. * @param integer $offset
  170. * @return mixed
  171. */
  172. final public static function iPos($needle, $haystack, $offset = 0) {
  173. return stripos($haystack, $needle, $offset);
  174. }
  175. /**
  176. * Find the position of the last occurrence of a substring in a string.
  177. * Note: use iLastPos for CI (for the sake of consistency and less arguments - already enough)
  178. *
  179. * @param string $needle
  180. * @param string $haystack
  181. * @param integer $offset
  182. * @return mixed
  183. */
  184. final public static function lastPos($needle, $haystack, $offset = 0) {
  185. return strrpos($haystack, $needle, $offset);
  186. }
  187. /**
  188. * Case-insensitive version of strrpos().
  189. *
  190. * @param string $needle
  191. * @param string $haystack
  192. * @param integer $offset
  193. * @return mixed
  194. */
  195. final public static function iLastPos($needle, $haystack, $offset = 0) {
  196. return strripos($haystack, $needle, $offset);
  197. }
  198. }