stringUtil.php 13 KB

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