Str.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 code($num = 4)
  57. {
  58. $code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  59. $rand = $code[rand(0,25)]
  60. .strtoupper(dechex(date('m')))
  61. .date('d')
  62. .substr(time(),-5)
  63. .substr(microtime(),2,5)
  64. .sprintf('%02d',rand(0,99));
  65. for(
  66. $a = md5( $rand, true ),
  67. $s = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ',
  68. $d = '',
  69. $f = 0;
  70. $f < $num;
  71. $g = ord( $a[ $f ] ),
  72. $d .= $s[ ( $g ^ ord( $a[ $f + 8 ] ) ) - $g & 0x1F ],
  73. $f++
  74. );
  75. return $d;
  76. }
  77. public static function uid($uid, $type = true, $salt = 123, $xorKey = 456)
  78. {
  79. $source = 'E5FCDG3HQA4B1NOPIJ2RSTUV67MWX89KLYZ';
  80. $base = strlen($source);
  81. $minLength = 6;
  82. if ($type) {
  83. $num = ($uid * $salt) ^ $xorKey;
  84. $code = '';
  85. while ($num > 0) {
  86. $mod = $num % $base;
  87. $num = intdiv($num, $base);
  88. $code = $source[$mod] . $code;
  89. }
  90. $padLen = $minLength - strlen($code);
  91. for ($i = 0; strlen($code) < $minLength; $i++) {
  92. $padChar = $source[($i * 7 + $uid) % $base];
  93. $code = $padChar . $code;
  94. }
  95. return $code . $padLen; // 补位长度加到最后1位,解码时识别
  96. } else {
  97. $padLen = (int)substr($uid, -1); // 最后一位是补位长度
  98. $uid = substr($uid, 0, -1); // 去掉末尾
  99. $uid = substr($uid, $padLen); // 去掉前面 padLen 个补位字符
  100. $num = 0;
  101. $len = strlen($uid);
  102. for ($i = 0; $i < $len; $i++) {
  103. $pos = strpos($source, $uid[$i]);
  104. if ($pos === false) return 0;
  105. $num = $num * $base + $pos;
  106. }
  107. $num = $num ^ $xorKey;
  108. return intdiv($num, $salt);
  109. }
  110. }
  111. public static function idtostr($id)
  112. {
  113. if (!is_numeric($id) || $id < 0) {
  114. return false;
  115. }
  116. $id = substr("00000000" . $id, -8);
  117. $sandNum = $id % 10;
  118. srand($id);
  119. $randstr = "" . rand(1, 9) . self::rand(7, 0);
  120. $retstr1 = "";
  121. $retstr2 = "";
  122. for ($i = 0; $i < 4; $i++) {
  123. $retstr1 .= $randstr[$i] . $id[$i];
  124. $retstr2 .= $id[7 - $i] . $randstr[7 - $i];
  125. }
  126. $retstr1 = substr(self::rand(6) . "g" . dechex($retstr1), -7);
  127. $retstr2 = substr(self::rand(6) . "g" . dechex($retstr2), -7);
  128. srand(time() + $id);
  129. $retstr = "1" . $sandNum;
  130. for ($i = 0; $i < 7; $i++) {
  131. $retstr .= $retstr1[$i] . $retstr2[$i];
  132. }
  133. return $retstr;
  134. }
  135. public static function strtoid($str)
  136. {
  137. if (strlen($str) != 16) {
  138. return $str;
  139. }
  140. //$type = $str1[0];
  141. $sandNum = $str[1];
  142. $retstr1 = $retstr2 = '';
  143. for ($i = 0; $i < 7; $i++) {
  144. if ($str[2+$i*2] == 'g') {
  145. $retstr1 = "";
  146. } else {
  147. $retstr1 .= $str[2+$i*2];
  148. }
  149. if ($str[3+$i*2] == 'g') {
  150. $retstr2 = "";
  151. } else {
  152. $retstr2 .= $str[3+$i*2];
  153. }
  154. }
  155. $retstr1 = "g".substr("00000000".hexdec($retstr1),-8);
  156. $retstr2 = "g".substr("00000000".hexdec($retstr2),-8);
  157. $ret1 = $ret2 = "";
  158. for ($i = 0; $i < 4; $i++) {
  159. $ret1 .= $retstr1[$i*2+2];
  160. $ret2 .= $retstr2[7-$i*2];
  161. }
  162. $ret = $ret1 * 10000 + $ret2;
  163. return $ret;
  164. }
  165. public static function cut($string, $length = 80, $etc = '...')
  166. {
  167. $result = '';
  168. $string = html_entity_decode(trim(strip_tags($string)), ENT_QUOTES, 'utf-8');
  169. for ($i = 0, $j = 0; $i < strlen($string); $i++) {
  170. if ($j >= $length) {
  171. for ($x = 0, $y = 0; $x < strlen($etc); $x++) {
  172. if ($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8, '0', STR_PAD_LEFT), '0')) {
  173. $x += $number - 1;
  174. $y++;
  175. } else {
  176. $y += 0.5;
  177. }
  178. }
  179. $length -= $y;
  180. break;
  181. }
  182. if ($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8, '0', STR_PAD_LEFT), '0')) {
  183. $i += $number - 1;
  184. $j++;
  185. } else {
  186. $j += 0.5;
  187. }
  188. }
  189. for ($i = 0; (($i < strlen($string)) && ($length > 0)); $i++) {
  190. if ($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8, '0', STR_PAD_LEFT), '0')) {
  191. if ($length < 1.0) {
  192. break;
  193. }
  194. $result .= substr($string, $i, $number);
  195. $length -= 1.0;
  196. $i += $number - 1;
  197. } else {
  198. $result .= substr($string, $i, 1);
  199. $length -= 0.5;
  200. }
  201. }
  202. //$result = htmlentities($result, ENT_QUOTES, 'utf-8');
  203. if ($i < strlen($string)) {
  204. $result .= $etc;
  205. }
  206. return $result;
  207. }
  208. public static function length($string)
  209. {
  210. preg_match_all("/./us", $string, $match);
  211. return count($match[0]);
  212. }
  213. public static function addstr($str, $index, $sub)
  214. {
  215. $str = self::explode($str, 1);
  216. $length = count($str);
  217. $num = floor($length / $index);
  218. for ($a = 1; $a <= $num; $a++) {
  219. $start = '';
  220. $b = $a * $index;
  221. foreach ($str as $k => $v) {
  222. if ($k == $b) {
  223. $str[$k] = $sub . $v;
  224. }
  225. }
  226. }
  227. return implode('', $str);
  228. }
  229. public static function explode($value, $num = 2)
  230. {
  231. $len = mb_strlen($value);
  232. $result = [];
  233. for ($i = 0; $i < $len; $i = $i + $num) {
  234. $result[$i / $num] = mb_substr($value, $i, $num);
  235. }
  236. return $result;
  237. }
  238. public static function ishtml($html)
  239. {
  240. if($html != strip_tags($html)) {
  241. return true;
  242. } else {
  243. return false;
  244. }
  245. }
  246. public static function encodePic($file, $type = 1)
  247. {
  248. $base64 = '';
  249. if (is_file($file)) {
  250. $info = getimagesize($file);
  251. $fp = fopen($file, "r");
  252. if ($fp) {
  253. $content = chunk_split(base64_encode(fread($fp, filesize($file))));
  254. switch ($info[2]) {
  255. case 1: $img_type = 'gif';
  256. break;
  257. case 2: $img_type = 'jpg';
  258. break;
  259. case 3: $img_type = 'png';
  260. break;
  261. }
  262. if ($type == 1) {
  263. $base64 = 'data:image/' . $img_type . ';base64,' . $content;
  264. } else {
  265. $base64 = $content;
  266. }
  267. }
  268. fclose($fp);
  269. }
  270. return $base64;
  271. }
  272. public static function getLink($text)
  273. {
  274. preg_match("/(https:|https:)(\/\/[A-Za-z0-9_#?.&=\/]+)([".chr(0xb0)."-".chr(0xf7)."][".chr(0xa1)."-".chr(0xfe)."])?(\s)?/i", $text, $result);
  275. if (isset($result[0])) {
  276. return $result[0];
  277. }
  278. return false;
  279. }
  280. public static function val($show, $data = [], $state = false)
  281. {
  282. if ($data && strpos($show, '{') !== false && strpos($show, '{"') === false) {
  283. $func = function ($r) use ($data) {
  284. if (isset($data[$r[1]])) {
  285. return $data[$r[1]];
  286. }
  287. return false;
  288. };
  289. $show = preg_replace_callback('/{(.*?)}/', $func, $show);
  290. }
  291. if (strstr($show, '"') || strstr($show, "'") || $state) {
  292. $eval = '$show = ' . $show . ';';
  293. } else {
  294. $eval = '$show = "' . $show . '";';
  295. }
  296. @eval($eval);
  297. if (is_numeric($show)) {
  298. $show = (float) $show;
  299. }
  300. return $show;
  301. }
  302. }