stringUtil.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. <?php
  2. namespace common\components;
  3. use Overtrue\Pinyin\Pinyin;
  4. use Yii;
  5. class stringUtil
  6. {
  7. const KEY = 'Aos4eLEmqpo8dmJleG610tR1KxUVQ5OzgkzpYM23PLqwSaU3Io';
  8. public static function passwordCheck($password)
  9. {
  10. if (preg_match('/^([0-9]{6,16})$/', $password)) {
  11. util::fail('密码不能纯数字');
  12. }
  13. if (preg_match('/^[a-z A-Z]{6,16}$/', $password)) {
  14. util::fail('密码不能纯字母');
  15. }
  16. if (strlen($password) < 6) {
  17. util::fail('密码必须大于6位');
  18. }
  19. }
  20. //随机编号,英文开头,用于鲜花商品编号
  21. public static function buildRandNo($id)
  22. {
  23. return 'F' . $id . time();// F == flower
  24. }
  25. //创建微信Token使用
  26. public static function buildWxToken($id)
  27. {
  28. return 'wt' . $id . time();
  29. }
  30. //订单号,用于购买商品生成的订单等
  31. public static function buildOrderNo($id = 0)
  32. {
  33. mt_srand((double)microtime() * 1000000);
  34. $no = empty($id) ? '' : $id;
  35. $no .= date('ymdHis') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
  36. return $no;
  37. }
  38. //判断是否手机号
  39. public static function isMobile($no)
  40. {
  41. return preg_match("/^1[0-9]\d{9}$/", $no) == true ? true : false;
  42. }
  43. public static function isEmail($email)
  44. {
  45. $reg = '/\w+@\w+(?:\.\w+){1,3}/i';
  46. if (preg_match($reg, $email)) {
  47. return true;
  48. }
  49. return false;
  50. }
  51. //是否所有都是英文字母 ssh 2021.1
  52. public static function isLetter($str)
  53. {
  54. if (preg_match("/^[a-zA-Z\s]+$/", $str)) {
  55. return true;
  56. }
  57. return false;
  58. }
  59. static function subStringUtf8($string, $length, $ext = '')
  60. {
  61. $num = self::getWordNum($string);
  62. //echo $num;exit();
  63. //如果截取字数大于字符长度
  64. if ($num <= $length) {
  65. return $string;
  66. }
  67. $str_length = $length * 3; //总的字节数
  68. $slen = 0;
  69. $i = 0;
  70. $count = 0;
  71. while ($i <= $str_length && $count < $length) {
  72. if (isset($string[$i])) {
  73. $c = ord($string[$i]);
  74. //第一个字节大于224的,它与它之后的2个字节一起组成一个UTF-8字符
  75. if ($c >= 224) {
  76. $bits = 3;
  77. $count++;
  78. //第一个字节大于192小于224的,它与它之后的1个字节组成一个UTF-8字符
  79. } elseif ($c >= 192) {
  80. $count++;
  81. $bits = 2;
  82. //否则第一个字节本身就是一个英文字符(包括数字和一小部分标点符号)
  83. } else {
  84. $bits = 1;
  85. $count += 0.5;
  86. }
  87. }
  88. $slen = $slen + $bits;
  89. $i = $i + $bits;
  90. }
  91. return substr($string, 0, $slen) . $ext;
  92. }
  93. //返回中文字数(二个字母会当一个汉字)
  94. static function getWordNum($string, $code = 'utf-8')
  95. {
  96. $i = 0; //开始字节数
  97. $count = 0; //开始字数
  98. if ($code == 'utf-8') { //utf-8编码
  99. while (isset($string[$i]) && ord($string[$i]) != 0) { //还没有到结尾
  100. $b = ord($string[$i]);
  101. //第一个字节大于224的,它与它之后的2个字节一起组成一个UTF-8字符
  102. if ($b >= 224) {
  103. $i += 3;
  104. $count++;
  105. //第一个字节大于192小于224的,它与它之后的1个字节组成一个UTF-8字符
  106. } elseif ($b >= 192) {
  107. $i += 2;
  108. $count++;
  109. //否则第一个字节本身就是一个英文字符(包括数字和一小部分标点符号)
  110. } else {
  111. $i += 1;
  112. $count += 0.5;
  113. }
  114. }
  115. return ceil($count);
  116. }
  117. //gkb编码
  118. while (isset($string[$i]) && ord($string[$i]) != 0) {
  119. $b = ord($string[$i]);
  120. //第一个字节ASCII码>129的,它与它之后的1个字节一起组成一个gbk字符。
  121. if ($b > 129) {
  122. $i += 2;
  123. $count++;
  124. //第一个字节ASCII码<=129的,一个字节组成一个gbk字符。
  125. } else {
  126. $i++;
  127. $count += 0.5;
  128. }
  129. }
  130. return ceil($count);
  131. }
  132. //取汉字拼音首字母
  133. //https://github.com/overtrue/pinyin
  134. public static function py($string)
  135. {
  136. if (empty($string)) {
  137. return '';
  138. }
  139. //如果包含中文
  140. if (preg_match('/[\x{4e00}-\x{9fa5}]/u', $string) > 0) {
  141. $pinyin = new Pinyin();
  142. $string = $pinyin->abbr($string, PINYIN_KEEP_ENGLISH);
  143. }
  144. return strtolower($string);
  145. }
  146. //取随机数
  147. static function charsShuffle($length)
  148. {
  149. $str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  150. $key = '';
  151. for ($i = 0; $i < $length; $i++) {
  152. $pos = mt_rand(0, strlen($str) - 1);
  153. $key .= $str[$pos];
  154. }
  155. return $key;
  156. }
  157. //取随机数(小写)
  158. static function charsShuffleLowerCase($length)
  159. {
  160. $str = 'abcdefghijklmnopqrstuvwxyz0123456789';
  161. $key = '';
  162. for ($i = 0; $i < $length; $i++) {
  163. $pos = mt_rand(0, strlen($str) - 1);
  164. $key .= $str[$pos];
  165. }
  166. return $key;
  167. }
  168. //随机取数字
  169. static function getRandNum($num)
  170. {
  171. $shuffle = str_shuffle('1234567890');
  172. $number = substr($shuffle, 0, $num);
  173. return $number;
  174. }
  175. // $string: 明文 或 密文
  176. // $operation:DECODE表示解密,其它表示加密
  177. // $key: 密匙
  178. // $expiry:密文有效期
  179. public function authcode($string, $operation = 'DECODE', $expiry = 0)
  180. {
  181. // 动态密匙长度,相同的明文会生成不同密文就是依靠动态密匙
  182. $ckey_length = 4;
  183. // 密匙
  184. $key = md5(self::KEY);
  185. // 密匙a会参与加解密
  186. $keya = md5(substr($key, 0, 16));
  187. // 密匙b会用来做数据完整性验证
  188. $keyb = md5(substr($key, 16, 16));
  189. // 密匙c用于变化生成的密文
  190. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length) : substr(md5(microtime()), -$ckey_length)) : '';
  191. // 参与运算的密匙
  192. $cryptkey = $keya . md5($keya . $keyc);
  193. $key_length = strlen($cryptkey);
  194. // 明文,前10位用来保存时间戳,解密时验证数据有效性,10到26位用来保存$keyb(密匙b),解密时会通过这个密匙验证数据完整性
  195. // 如果是解码的话,会从第$ckey_length位开始,因为密文前$ckey_length位保存 动态密匙,以保证解密正确
  196. $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0) . substr(md5($string . $keyb), 0, 16) . $string;
  197. $string_length = strlen($string);
  198. $result = '';
  199. $box = range(0, 255);
  200. $rndkey = array();
  201. // 产生密匙簿
  202. for ($i = 0; $i <= 255; $i++) {
  203. $rndkey[$i] = ord($cryptkey[$i % $key_length]);
  204. }
  205. // 用固定的算法,打乱密匙簿,增加随机性,好像很复杂,实际上对并不会增加密文的强度
  206. for ($j = $i = 0; $i < 256; $i++) {
  207. $j = ($j + $box[$i] + $rndkey[$i]) % 256;
  208. $tmp = $box[$i];
  209. $box[$i] = $box[$j];
  210. $box[$j] = $tmp;
  211. }
  212. // 核心加解密部分
  213. for ($a = $j = $i = 0; $i < $string_length; $i++) {
  214. $a = ($a + 1) % 256;
  215. $j = ($j + $box[$a]) % 256;
  216. $tmp = $box[$a];
  217. $box[$a] = $box[$j];
  218. $box[$j] = $tmp;
  219. // 从密匙簿得出密匙进行异或,再转成字符
  220. $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
  221. }
  222. if ($operation == 'DECODE') {
  223. // substr($result, 0, 10) == 0 验证数据有效性
  224. // substr($result, 0, 10) - time() > 0 验证数据有效性
  225. // substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16) 验证数据完整性
  226. // 验证数据有效性,请看未加密明文的格式
  227. if ((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26) . $keyb), 0, 16)) {
  228. return substr($result, 26);
  229. } else {
  230. return '';
  231. }
  232. } else {
  233. // 把动态密匙保存在密文里,这也是为什么同样的明文,生产不同密文后能解密的原因
  234. // 因为加密后的密文可能是一些特殊字符,复制过程可能会丢失,所以用base64编码
  235. return $keyc . str_replace('=', '', base64_encode($result));
  236. }
  237. }
  238. /**
  239. * 返回8位随机字符,重复概率较低
  240. */
  241. public static function getRandStr($id = null)
  242. {
  243. $chars = "0123456789abcdefghijklmnopqrstuvwxyz";
  244. $res = "";
  245. for ($i = 0; $i < 8; $i++) {
  246. $res .= $chars[mt_rand(0, strlen($chars) - 1)];
  247. }
  248. $rand = empty($id) ? $res : $res . $id;
  249. return $rand;
  250. }
  251. public static function randIP()
  252. {
  253. $arr_1 = array("218", "218", "66", "66", "218", "218", "60", "60", "202", "204", "66", "66", "66", "59", "61", "60", "222", "221", "66", "59", "60", "60", "66", "218", "218", "62", "63", "64", "66", "66", "122", "211");
  254. $randarr = mt_rand(0, count($arr_1));
  255. $ip1id = $arr_1[$randarr];
  256. $ip2id = round(rand(600000, 2550000) / 10000);
  257. $ip3id = round(rand(600000, 2550000) / 10000);
  258. $ip4id = round(rand(600000, 2550000) / 10000);
  259. return $ip1id . "." . $ip2id . "." . $ip3id . "." . $ip4id;
  260. }
  261. //写日志
  262. public static function log($content, $file)
  263. {
  264. $content .= "\r\n";
  265. $fh = fopen($file, 'ab'); //打开,追加模式
  266. fwrite($fh, $content);
  267. fclose($fh);
  268. }
  269. //减法
  270. public static function calcSub($a, $b)
  271. {
  272. return ($a * 1000 - $b * 1000) / 1000;
  273. }
  274. //加法
  275. public static function calcAdd($a, $b)
  276. {
  277. return ($a * 1000 + $b * 1000) / 1000;
  278. }
  279. public static function ride($a, $b)
  280. {
  281. }
  282. /**
  283. * 生成商家提现需要使用的订单号
  284. */
  285. public static function generateMerchantCashOrderNo($sjId)
  286. {
  287. return "C" . date('ymdHis') . $sjId . mt_rand(10000, 99999);
  288. }
  289. //生成用户需要使用的订单号 ssh 2019.4.20
  290. public static function generateOrderNo($sjId, $userId)
  291. {
  292. $userId = empty($userId) ? rand(12013120, 12536120) : $userId;
  293. if (strlen($userId) < 8) {
  294. exit('用户ID少于8位数');
  295. }
  296. $no = date('ymdHis') . $sjId . $userId . mt_rand(10000, 99999);
  297. if (strlen($no) > 32) {
  298. exit('订单位数大于32位');
  299. }
  300. return $no;
  301. }
  302. //微信32位的场景ID ssh 2019.9.4
  303. public static function generateSceneId($sjId)
  304. {
  305. $no = 'S' . date('YmdHis') . $sjId . mt_rand(100000, 999999);
  306. if (strlen($no) > 32) {
  307. $no = substr($no, 0, 32);
  308. }
  309. return $no;
  310. }
  311. //将加密窜转成可以在http上传输的字符
  312. public static function urlSafeB64Encode($string)
  313. {
  314. $data = base64_encode($string);
  315. $data = str_replace(array('+', '/', '='), array('-', '_', ''), $data);
  316. return $data;
  317. }
  318. //将url上传输的字符解密
  319. public static function urlSafeBase64Decode($string)
  320. {
  321. $data = str_replace(array('-', '_'), array('+', '/'), $string);
  322. $mod4 = strlen($data) % 4;
  323. if ($mod4) {
  324. $data .= substr('====', $mod4);
  325. }
  326. return base64_decode($data);
  327. }
  328. //中文二边保留,中间用*号代替 ssh 2019.8.13
  329. public static function replaceXH($name)
  330. {
  331. //获取字符串长度
  332. $strlen = mb_strlen($name, 'utf-8');
  333. //如果字符创长度小于2,不做任何处理
  334. if ($strlen < 2) {
  335. return $name;
  336. } else {
  337. //mb_substr — 获取字符串的部分
  338. $firstStr = mb_substr($name, 0, 1, 'utf-8');
  339. $lastStr = mb_substr($name, -1, 1, 'utf-8');
  340. //str_repeat — 重复一个字符串
  341. return $strlen == 2 ? $firstStr . str_repeat('*', mb_strlen($name, 'utf-8') - 1) : $firstStr . str_repeat("*", $strlen - 2) . $lastStr;
  342. }
  343. }
  344. //生成唯一的文件名称 ssh 2020.1.7
  345. public static function uniqueFileName()
  346. {
  347. //参考 https://www.cnblogs.com/yjf512/p/9057229.html
  348. //https://www.cnblogs.com/roluce/p/6026081.html
  349. return md5(uniqid(mt_rand(), true));
  350. }
  351. //生成短的随机名称,基于当前时间微秒数 ssh 2020.4.13
  352. public static function shortUniqueId($id)
  353. {
  354. //示例:05e946fc1008bc
  355. return uniqid($id);
  356. }
  357. //参数格式化,错误提示查看友好 ssh 2021.01.04
  358. public static function paramsFriend($params)
  359. {
  360. $str = "[";
  361. if (!empty($params) && is_array($params)) {
  362. foreach ($params as $name => $item) {
  363. $item = is_array($item) ? json_encode($item) : $item;
  364. $str .= "\n{$name}=>{$item}";
  365. }
  366. }
  367. $str .= "\n]";
  368. return $str;
  369. }
  370. }