/Lib/Utility/Str.php

https://github.com/Dismounted/tools · PHP · 214 lines · 50 code · 17 blank · 147 comment · 1 complexity · 87fa6598f204e7cebe88d0abc6a364b6 MD5 · raw file

  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. */
  27. final public function __construct() {
  28. }
  29. /**
  30. * Find the first occurrence of a string.
  31. * Note: use iStr for CI
  32. *
  33. * @param mixed $needle
  34. * @param string $haystack
  35. * @param bool $beforeNeedle (defaults to false)
  36. * @return mixed
  37. */
  38. final public static function str($needle, $haystack, $beforeNeedle = false) {
  39. return strstr($haystack, $needle, $beforeNeedle);
  40. }
  41. /**
  42. * Case-insensitive strstr().
  43. *
  44. * @param mixed $needle
  45. * @param string $haystack
  46. * @param bool $beforeNeedle (defaults to false)
  47. * @return mixed
  48. */
  49. final public static function iStr($needle, $haystack, $beforeNeedle = false) {
  50. return stristr($haystack, $needle, $beforeNeedle);
  51. }
  52. /**
  53. * Find the first occurrence of a string - alias of strstr().
  54. *
  55. * @param mixed $needle
  56. * @param string $haystack
  57. * @param bool $beforeNeedle (defaults to false)
  58. * @return mixed
  59. */
  60. final public static function chr($needle, $haystack, $beforeNeedle = false) {
  61. return strchr($haystack, $needle, $beforeNeedle);
  62. }
  63. /**
  64. * Find the last occurrence of a character in a string.
  65. * Note: If needle contains more than one character, only the first is used.
  66. * This behavior is different from that of strstr(). This behavior is different from that of strstr().
  67. * If needle is not a string, it is converted to an integer and applied as the ordinal value of a character.
  68. *
  69. * @param mixed $needle
  70. * @param string $haystack
  71. * @return mixed
  72. */
  73. final public static function lastChr($needle, $haystack) {
  74. return strrchr($haystack, $needle);
  75. }
  76. /**
  77. * Replace all occurrences of the search string with the replacement string.
  78. * Note: use iReplace for CI
  79. *
  80. * @param mixed $search
  81. * @param mixed $replace
  82. * @param mixed $subject
  83. * @param int $count Reference to store count in
  84. * @return mixed
  85. */
  86. final public static function replace($search, $replace, $subject, &$count = null) {
  87. return str_replace($search, $replace, $subject, $count);
  88. }
  89. /**
  90. * Case-insensitive version of str_replace().
  91. *
  92. * @param mixed $search
  93. * @param mixed $replace
  94. * @param mixed $subject
  95. * @param int $count Reference to store count in
  96. * @return mixed
  97. */
  98. final public static function iReplace($search, $replace, $subject, &$count = null) {
  99. return str_ireplace($search, $replace, $subject, $count);
  100. }
  101. /**
  102. * Replace text within a portion of a string.
  103. *
  104. * @param mixed $string
  105. * @param string $replacement
  106. * @return mixed
  107. */
  108. final public static function substrReplace($string, $replacement, $start, $length = null) {
  109. return substr_replace($string, $replacement, $start, $length);
  110. }
  111. /**
  112. * Count the number of substring occurrences.
  113. *
  114. * @param string $needle
  115. * @param string $haystack
  116. * @param int $offset
  117. * @param int $length
  118. * @return int
  119. */
  120. final public static function count($needle, $haystack, $offset = 0, $length = null) {
  121. if ($length === null) {
  122. return substr_count($haystack, $needle, $offset);
  123. }
  124. return substr_count($haystack, $needle, $offset, $length);
  125. }
  126. /**
  127. * Binary safe comparison of two strings from an offset, up to length characters.
  128. * Note: use iCompare for CI (for the sake of consistency and less arguments - already enough)
  129. *
  130. * @param string $mainStr
  131. * @param string $str
  132. * @param int $offset
  133. * @param int $length
  134. * @return mixed
  135. */
  136. final public static function compare($mainStr, $str, $offset = 0, $length = null) {
  137. return substr_compare($mainStr, $str, $offset, $length);
  138. }
  139. /**
  140. * Binary safe comparison of two strings from an offset, up to length characters.
  141. *
  142. * @param string $mainStr
  143. * @param string $str
  144. * @param int $offset
  145. * @param int $length
  146. * @return mixed
  147. */
  148. final public static function iCompare($mainStr, $str, $offset = 0, $length = null) {
  149. return substr_compare($mainStr, $str, $offset, $length, true);
  150. }
  151. /**
  152. * Find the position of the first occurrence of a substring in a string.
  153. * Note: use iPos for CI (for the sake of consistency and less arguments - already enough)
  154. *
  155. * @param string $needle
  156. * @param string $haystack
  157. * @param int $offset
  158. * @return mixed
  159. */
  160. final public static function pos($needle, $haystack, $offset = 0) {
  161. return strpos($haystack, $needle, $offset);
  162. }
  163. /**
  164. * Case-insensitive version of stripos().
  165. *
  166. * @param string $needle
  167. * @param string $haystack
  168. * @param int $offset
  169. * @return mixed
  170. */
  171. final public static function iPos($needle, $haystack, $offset = 0) {
  172. return stripos($haystack, $needle, $offset);
  173. }
  174. /**
  175. * Find the position of the last occurrence of a substring in a string.
  176. * Note: use iLastPos for CI (for the sake of consistency and less arguments - already enough)
  177. *
  178. * @param string $needle
  179. * @param string $haystack
  180. * @param int $offset
  181. * @return mixed
  182. */
  183. final public static function lastPos($needle, $haystack, $offset = 0) {
  184. return strrpos($haystack, $needle, $offset);
  185. }
  186. /**
  187. * Case-insensitive version of strrpos().
  188. *
  189. * @param string $needle
  190. * @param string $haystack
  191. * @param int $offset
  192. * @return mixed
  193. */
  194. final public static function iLastPos($needle, $haystack, $offset = 0) {
  195. return strripos($haystack, $needle, $offset);
  196. }
  197. }