stringUtil.php 13 KB

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