Str.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php namespace Dever\Helper;
  2. class Str
  3. {
  4. public static function encode($string, $key = '')
  5. {
  6. return Secure::encode($string, $key);
  7. }
  8. public static function decode($string, $key = '')
  9. {
  10. return Secure::decode($string, $key);
  11. }
  12. public static function hide($string, $start = 3, $len = 4, $hide = '****')
  13. {
  14. return substr_replace($string, $hide, $start, $len);
  15. }
  16. public static function salt($len)
  17. {
  18. return bin2hex(random_bytes($len));
  19. }
  20. public static function rand($len, $type = 4)
  21. {
  22. $source = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"];
  23. $config = array (
  24. 0 => ["min" => 0, "max" => 9], /// 全数字
  25. 1 => ["min" => 10, "max" => 35], /// 全小写
  26. 2 => ["min" => 36, "max" => 61], /// 全大写
  27. 3 => ["min" => 10, "max" => 61], /// 大小写
  28. 4 => ["min" => 0, "max" => 61], /// 数字+大小写
  29. );
  30. if (!isset($config[$type])) {
  31. $type = 4;
  32. }
  33. $rand = "";
  34. for ($i = 0; $i < $len; $i++) {
  35. $rand .= $source[rand($config[$type]["min"], $config[$type]["max"])];
  36. }
  37. return $rand;
  38. }
  39. public static function order($prefix = '', $type = 1)
  40. {
  41. if ($type == 1) {
  42. return $prefix . date('Ymd').substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(1000, 9999));
  43. } elseif ($type == 2) {
  44. return $prefix . time() . substr(microtime(), 2, 5) . sprintf('%02d', rand(100000, 999999));
  45. } elseif ($type == 3) {
  46. return $prefix . date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
  47. } elseif ($type == 4) {
  48. if (function_exists('session_create_id')) {
  49. return $prefix . strtolower(session_create_id());
  50. } else {
  51. $charid = strtolower(md5(uniqid(mt_rand(), true)));
  52. return $prefix . substr($charid, 0, 8) . substr($charid, 8, 4) . substr($charid, 12, 4) . substr($charid, 16, 4) . substr($charid, 20, 12);
  53. }
  54. }
  55. }
  56. public static function orderNum($id)
  57. {
  58. // 时间戳(到秒)
  59. $time = date('YmdHis');
  60. // 两位随机(36进制,36^2 = 1296)
  61. $rand = self::base36(mt_rand(0, 1295));
  62. // 转为大写,固定两位
  63. $rand = str_pad($rand, 2, '0', STR_PAD_LEFT);
  64. // ID 的 36 进制
  65. $id36 = self::base36($id);
  66. return $time . $rand . $id36;
  67. }
  68. public static function base36($num)
  69. {
  70. $chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  71. $result = '';
  72. while ($num > 0) {
  73. $result = $chars[$num % 36] . $result;
  74. $num = intval($num / 36);
  75. }
  76. return $result ?: '0';
  77. }
  78. public static function code($num = 4)
  79. {
  80. $code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  81. $rand = $code[rand(0,25)]
  82. .strtoupper(dechex(date('m')))
  83. .date('d')
  84. .substr(time(),-5)
  85. .substr(microtime(),2,5)
  86. .sprintf('%02d',rand(0,99));
  87. for(
  88. $a = md5( $rand, true ),
  89. $s = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ',
  90. $d = '',
  91. $f = 0;
  92. $f < $num;
  93. $g = ord( $a[ $f ] ),
  94. $d .= $s[ ( $g ^ ord( $a[ $f + 8 ] ) ) - $g & 0x1F ],
  95. $f++
  96. );
  97. return $d;
  98. }
  99. public static function uid($uid, $type = true, $salt = 123, $xorKey = 456)
  100. {
  101. $source = 'E5FCDG3HQA4B1NOPIJ2RSTUV67MWX89KLYZ';
  102. $base = strlen($source);
  103. $minLength = 6;
  104. if ($type) {
  105. $num = ($uid * $salt) ^ $xorKey;
  106. $code = '';
  107. while ($num > 0) {
  108. $mod = $num % $base;
  109. $num = intdiv($num, $base);
  110. $code = $source[$mod] . $code;
  111. }
  112. $padLen = $minLength - strlen($code);
  113. for ($i = 0; strlen($code) < $minLength; $i++) {
  114. $padChar = $source[($i * 7 + $uid) % $base];
  115. $code = $padChar . $code;
  116. }
  117. return $code . $padLen; // 补位长度加到最后1位,解码时识别
  118. } else {
  119. $padLen = (int)substr($uid, -1); // 最后一位是补位长度
  120. $uid = substr($uid, 0, -1); // 去掉末尾
  121. $uid = substr($uid, $padLen); // 去掉前面 padLen 个补位字符
  122. $num = 0;
  123. $len = strlen($uid);
  124. for ($i = 0; $i < $len; $i++) {
  125. $pos = strpos($source, $uid[$i]);
  126. if ($pos === false) return 0;
  127. $num = $num * $base + $pos;
  128. }
  129. $num = $num ^ $xorKey;
  130. return intdiv($num, $salt);
  131. }
  132. }
  133. public static function idtostr($id)
  134. {
  135. if (!is_numeric($id) || $id < 0) {
  136. return false;
  137. }
  138. $id = substr("00000000" . $id, -8);
  139. $sandNum = $id % 10;
  140. srand($id);
  141. $randstr = "" . rand(1, 9) . self::rand(7, 0);
  142. $retstr1 = "";
  143. $retstr2 = "";
  144. for ($i = 0; $i < 4; $i++) {
  145. $retstr1 .= $randstr[$i] . $id[$i];
  146. $retstr2 .= $id[7 - $i] . $randstr[7 - $i];
  147. }
  148. $retstr1 = substr(self::rand(6) . "g" . dechex($retstr1), -7);
  149. $retstr2 = substr(self::rand(6) . "g" . dechex($retstr2), -7);
  150. srand(time() + $id);
  151. $retstr = "1" . $sandNum;
  152. for ($i = 0; $i < 7; $i++) {
  153. $retstr .= $retstr1[$i] . $retstr2[$i];
  154. }
  155. return $retstr;
  156. }
  157. public static function strtoid($str)
  158. {
  159. if (strlen($str) != 16) {
  160. return $str;
  161. }
  162. //$type = $str1[0];
  163. $sandNum = $str[1];
  164. $retstr1 = $retstr2 = '';
  165. for ($i = 0; $i < 7; $i++) {
  166. if ($str[2+$i*2] == 'g') {
  167. $retstr1 = "";
  168. } else {
  169. $retstr1 .= $str[2+$i*2];
  170. }
  171. if ($str[3+$i*2] == 'g') {
  172. $retstr2 = "";
  173. } else {
  174. $retstr2 .= $str[3+$i*2];
  175. }
  176. }
  177. $retstr1 = "g".substr("00000000".hexdec($retstr1),-8);
  178. $retstr2 = "g".substr("00000000".hexdec($retstr2),-8);
  179. $ret1 = $ret2 = "";
  180. for ($i = 0; $i < 4; $i++) {
  181. $ret1 .= $retstr1[$i*2+2];
  182. $ret2 .= $retstr2[7-$i*2];
  183. }
  184. $ret = $ret1 * 10000 + $ret2;
  185. return $ret;
  186. }
  187. public static function cut($string, $length = 80, $etc = '...')
  188. {
  189. $result = '';
  190. $string = html_entity_decode(trim(strip_tags($string)), ENT_QUOTES, 'utf-8');
  191. for ($i = 0, $j = 0; $i < strlen($string); $i++) {
  192. if ($j >= $length) {
  193. for ($x = 0, $y = 0; $x < strlen($etc); $x++) {
  194. if ($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8, '0', STR_PAD_LEFT), '0')) {
  195. $x += $number - 1;
  196. $y++;
  197. } else {
  198. $y += 0.5;
  199. }
  200. }
  201. $length -= $y;
  202. break;
  203. }
  204. if ($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8, '0', STR_PAD_LEFT), '0')) {
  205. $i += $number - 1;
  206. $j++;
  207. } else {
  208. $j += 0.5;
  209. }
  210. }
  211. for ($i = 0; (($i < strlen($string)) && ($length > 0)); $i++) {
  212. if ($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8, '0', STR_PAD_LEFT), '0')) {
  213. if ($length < 1.0) {
  214. break;
  215. }
  216. $result .= substr($string, $i, $number);
  217. $length -= 1.0;
  218. $i += $number - 1;
  219. } else {
  220. $result .= substr($string, $i, 1);
  221. $length -= 0.5;
  222. }
  223. }
  224. //$result = htmlentities($result, ENT_QUOTES, 'utf-8');
  225. if ($i < strlen($string)) {
  226. $result .= $etc;
  227. }
  228. return $result;
  229. }
  230. public static function length($string)
  231. {
  232. preg_match_all("/./us", $string, $match);
  233. return count($match[0]);
  234. }
  235. public static function addstr($str, $index, $sub)
  236. {
  237. $str = self::explode($str, 1);
  238. $length = count($str);
  239. $num = floor($length / $index);
  240. for ($a = 1; $a <= $num; $a++) {
  241. $start = '';
  242. $b = $a * $index;
  243. foreach ($str as $k => $v) {
  244. if ($k == $b) {
  245. $str[$k] = $sub . $v;
  246. }
  247. }
  248. }
  249. return implode('', $str);
  250. }
  251. public static function explode($value, $num = 2)
  252. {
  253. $len = mb_strlen($value);
  254. $result = [];
  255. for ($i = 0; $i < $len; $i = $i + $num) {
  256. $result[$i / $num] = mb_substr($value, $i, $num);
  257. }
  258. return $result;
  259. }
  260. public static function ishtml($html)
  261. {
  262. if($html != strip_tags($html)) {
  263. return true;
  264. } else {
  265. return false;
  266. }
  267. }
  268. public static function encodePic($file, $type = 1)
  269. {
  270. $base64 = '';
  271. if (is_file($file)) {
  272. $info = getimagesize($file);
  273. $fp = fopen($file, "r");
  274. if ($fp) {
  275. $content = chunk_split(base64_encode(fread($fp, filesize($file))));
  276. switch ($info[2]) {
  277. case 1: $img_type = 'gif';
  278. break;
  279. case 2: $img_type = 'jpg';
  280. break;
  281. case 3: $img_type = 'png';
  282. break;
  283. }
  284. if ($type == 1) {
  285. $base64 = 'data:image/' . $img_type . ';base64,' . $content;
  286. } else {
  287. $base64 = $content;
  288. }
  289. }
  290. fclose($fp);
  291. }
  292. return $base64;
  293. }
  294. public static function getLink($text)
  295. {
  296. preg_match("/(https:|https:)(\/\/[A-Za-z0-9_#?.&=\/]+)([".chr(0xb0)."-".chr(0xf7)."][".chr(0xa1)."-".chr(0xfe)."])?(\s)?/i", $text, $result);
  297. if (isset($result[0])) {
  298. return $result[0];
  299. }
  300. return false;
  301. }
  302. public static function val($show, $data = [], $state = false)
  303. {
  304. if (is_array($data) && strpos($show, '{') !== false && strpos($show, '{"') === false) {
  305. $func = function ($r) use ($data) {
  306. if (isset($data[$r[1]])) {
  307. return $data[$r[1]];
  308. }
  309. return false;
  310. };
  311. $show = preg_replace_callback('/{(.*?)}/', $func, $show);
  312. }
  313. if (strstr($show, '"') || strstr($show, "'") || $state) {
  314. $eval = '$show = ' . $show . ';';
  315. } else {
  316. $eval = '$show = "' . $show . '";';
  317. }
  318. @eval($eval);
  319. if (is_numeric($show)) {
  320. $show = (float) $show;
  321. }
  322. return $show;
  323. }
  324. }