Str.php 11 KB

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