Str.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: yunwuxin <448901948@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\helper;
  12. class Str
  13. {
  14. protected static $snakeCache = [];
  15. protected static $camelCache = [];
  16. protected static $studlyCache = [];
  17. /**
  18. * 检查字符串中是否包含某些字符串
  19. * @param string $haystack
  20. * @param string|array $needles
  21. * @return bool
  22. */
  23. public static function contains(string $haystack, $needles): bool
  24. {
  25. foreach ((array) $needles as $needle) {
  26. if ('' != $needle && mb_strpos($haystack, $needle) !== false) {
  27. return true;
  28. }
  29. }
  30. return false;
  31. }
  32. /**
  33. * 检查字符串是否以某些字符串结尾
  34. *
  35. * @param string $haystack
  36. * @param string|array $needles
  37. * @return bool
  38. */
  39. public static function endsWith(string $haystack, $needles): bool
  40. {
  41. foreach ((array) $needles as $needle) {
  42. if ((string) $needle === static::substr($haystack, -static::length($needle))) {
  43. return true;
  44. }
  45. }
  46. return false;
  47. }
  48. /**
  49. * 检查字符串是否以某些字符串开头
  50. *
  51. * @param string $haystack
  52. * @param string|array $needles
  53. * @return bool
  54. */
  55. public static function startsWith(string $haystack, $needles): bool
  56. {
  57. foreach ((array) $needles as $needle) {
  58. if ('' != $needle && mb_strpos($haystack, $needle) === 0) {
  59. return true;
  60. }
  61. }
  62. return false;
  63. }
  64. /**
  65. * 获取指定长度的随机字母数字组合的字符串
  66. *
  67. * @param int $length
  68. * @param int $type
  69. * @param string $addChars
  70. * @return string
  71. */
  72. public static function random(int $length = 6, int $type = null, string $addChars = ''): string
  73. {
  74. $str = '';
  75. switch ($type) {
  76. case 0:
  77. $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' . $addChars;
  78. break;
  79. case 1:
  80. $chars = str_repeat('0123456789', 3);
  81. break;
  82. case 2:
  83. $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' . $addChars;
  84. break;
  85. case 3:
  86. $chars = 'abcdefghijklmnopqrstuvwxyz' . $addChars;
  87. break;
  88. case 4:
  89. $chars = "们以我到他会作时要动国产的一是工就年阶义发成部民可出能方进在了不和有大这主中人上为来分生对于学下级地个用同行面说种过命度革而多子后自社加小机也经力线本电高量长党得实家定深法表着水理化争现所二起政三好十战无农使性前等反体合斗路图把结第里正新开论之物从当两些还天资事队批点育重其思与间内去因件日利相由压员气业代全组数果期导平各基或月毛然如应形想制心样干都向变关问比展那它最及外没看治提五解系林者米群头意只明四道马认次文通但条较克又公孔领军流入接席位情运器并飞原油放立题质指建区验活众很教决特此常石强极土少已根共直团统式转别造切九你取西持总料连任志观调七么山程百报更见必真保热委手改管处己将修支识病象几先老光专什六型具示复安带每东增则完风回南广劳轮科北打积车计给节做务被整联步类集号列温装即毫知轴研单色坚据速防史拉世设达尔场织历花受求传口断况采精金界品判参层止边清至万确究书" . $addChars;
  90. break;
  91. default:
  92. $chars = 'ABCDEFGHIJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789' . $addChars;
  93. break;
  94. }
  95. if ($length > 10) {
  96. $chars = $type == 1 ? str_repeat($chars, $length) : str_repeat($chars, 5);
  97. }
  98. if ($type != 4) {
  99. $chars = str_shuffle($chars);
  100. $str = substr($chars, 0, $length);
  101. } else {
  102. for ($i = 0; $i < $length; $i++) {
  103. $str .= mb_substr($chars, floor(mt_rand(0, mb_strlen($chars, 'utf-8') - 1)), 1);
  104. }
  105. }
  106. return $str;
  107. }
  108. /**
  109. * 字符串转小写
  110. *
  111. * @param string $value
  112. * @return string
  113. */
  114. public static function lower(string $value): string
  115. {
  116. return mb_strtolower($value, 'UTF-8');
  117. }
  118. /**
  119. * 字符串转大写
  120. *
  121. * @param string $value
  122. * @return string
  123. */
  124. public static function upper(string $value): string
  125. {
  126. return mb_strtoupper($value, 'UTF-8');
  127. }
  128. /**
  129. * 获取字符串的长度
  130. *
  131. * @param string $value
  132. * @return int
  133. */
  134. public static function length(string $value): int
  135. {
  136. return mb_strlen($value);
  137. }
  138. /**
  139. * 截取字符串
  140. *
  141. * @param string $string
  142. * @param int $start
  143. * @param int|null $length
  144. * @return string
  145. */
  146. public static function substr(string $string, int $start, int $length = null): string
  147. {
  148. return mb_substr($string, $start, $length, 'UTF-8');
  149. }
  150. /**
  151. * 驼峰转下划线
  152. *
  153. * @param string $value
  154. * @param string $delimiter
  155. * @return string
  156. */
  157. public static function snake(string $value, string $delimiter = '_'): string
  158. {
  159. $key = $value;
  160. if (isset(static::$snakeCache[$key][$delimiter])) {
  161. return static::$snakeCache[$key][$delimiter];
  162. }
  163. if (!ctype_lower($value)) {
  164. $value = preg_replace('/\s+/u', '', ucwords($value));
  165. $value = static::lower(preg_replace('/(.)(?=[A-Z])/u', '$1' . $delimiter, $value));
  166. }
  167. return static::$snakeCache[$key][$delimiter] = $value;
  168. }
  169. /**
  170. * 下划线转驼峰(首字母小写)
  171. *
  172. * @param string $value
  173. * @return string
  174. */
  175. public static function camel(string $value): string
  176. {
  177. if (isset(static::$camelCache[$value])) {
  178. return static::$camelCache[$value];
  179. }
  180. return static::$camelCache[$value] = lcfirst(static::studly($value));
  181. }
  182. /**
  183. * 下划线转驼峰(首字母大写)
  184. *
  185. * @param string $value
  186. * @return string
  187. */
  188. public static function studly(string $value): string
  189. {
  190. $key = $value;
  191. if (isset(static::$studlyCache[$key])) {
  192. return static::$studlyCache[$key];
  193. }
  194. $value = ucwords(str_replace(['-', '_'], ' ', $value));
  195. return static::$studlyCache[$key] = str_replace(' ', '', $value);
  196. }
  197. /**
  198. * 转为首字母大写的标题格式
  199. *
  200. * @param string $value
  201. * @return string
  202. */
  203. public static function title(string $value): string
  204. {
  205. return mb_convert_case($value, MB_CASE_TITLE, 'UTF-8');
  206. }
  207. }