Str.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. * 2012-04-13 ms
  11. */
  12. final class Str {
  13. /**
  14. * Avoid constructor conflicts.
  15. *
  16. * @return void
  17. */
  18. final public function __construct() {
  19. }
  20. /**
  21. * Find the first occurrence of a string.
  22. * Note: use iStr for CI
  23. *
  24. * @return mixed
  25. */
  26. final public static function str($needle, $haystack, $beforeNeedle = false) {
  27. return strstr($haystack, $needle, $beforeNeedle);
  28. }
  29. /**
  30. * Case-insensitive strstr().
  31. *
  32. * @return mixed
  33. */
  34. final public static function iStr($needle, $haystack, $beforeNeedle = false) {
  35. return stristr($haystack, $needle, $beforeNeedle);
  36. }
  37. /**
  38. * Find the first occurrence of a string - alias of strstr().
  39. *
  40. * @return mixed
  41. */
  42. final public static function chr($needle, $haystack, $beforeNeedle = false) {
  43. return strchr($haystack, $needle, $beforeNeedle);
  44. }
  45. /**
  46. * Find the last occurrence of a character in a string.
  47. * Note: If needle contains more than one character, only the first is used.
  48. * This behavior is different from that of strstr(). This behavior is different from that of strstr().
  49. * If needle is not a string, it is converted to an integer and applied as the ordinal value of a character.
  50. *
  51. * @return mixed
  52. */
  53. final public static function rChr($needle, $haystack) {
  54. return strrchr($haystack, $needle);
  55. }
  56. /**
  57. * Replace all occurrences of the search string with the replacement string.
  58. * Note: use iReplace for CI
  59. *
  60. * @return mixed
  61. */
  62. final public static function replace($search, $replace, $subject, &$count = null) {
  63. return str_replace($search, $replace, $subject, $count);
  64. }
  65. /**
  66. * Case-insensitive version of str_replace().
  67. *
  68. * @return mixed
  69. */
  70. final public static function iReplace($search, $replace, $subject, &$count = null) {
  71. return str_ireplace($search, $replace, $subject, $count);
  72. }
  73. /**
  74. * Replace text within a portion of a string.
  75. *
  76. * @return mixed
  77. */
  78. final public static function substrReplace($string, $replacement, $start, $length = null) {
  79. return substr_replace($string, $replacement, $start, $length);
  80. }
  81. /**
  82. * Count the number of substring occurrences.
  83. *
  84. * @return int
  85. */
  86. final public static function count($needle, $haystack, $offset = 0, $length = null) {
  87. return substr_count($needle, $haystack, $offset = 0, $length);
  88. }
  89. /**
  90. * Binary safe comparison of two strings from an offset, up to length characters.
  91. * Note: use iCompare for CI (for the sake of consistency and less arguments - already enough)
  92. *
  93. * @return mixed
  94. */
  95. final public static function compare($mainStr, $str, $offset = 0, $length = null) {
  96. return substr_compare($mainStr, $str, $offset = 0, $length);
  97. }
  98. /**
  99. * Binary safe comparison of two strings from an offset, up to length characters.
  100. *
  101. * @return mixed
  102. */
  103. final public static function iCompare($mainStr, $str, $offset = 0, $length = null) {
  104. return substr_compare($needle, $haystack, $offset = 0, $length, true);
  105. }
  106. /**
  107. * Find the position of the first occurrence of a substring in a string.
  108. *
  109. * @return mixed
  110. */
  111. final public static function pos($needle, $haystack, $offset = 0) {
  112. return strpos($haystack, $needle, $offset);
  113. }
  114. /**
  115. * Find the position of the last occurrence of a substring in a string.
  116. *
  117. * @return mixed
  118. */
  119. final public static function rPos($needle, $haystack, $offset = 0) {
  120. return strrpos($haystack, $needle, $offset);
  121. }
  122. }