Str.php 5.9 KB

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