StringHelper.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. /**
  3. * @link http://www.tpframe.com/
  4. * @copyright Copyright (c) 2017 TPFrame Software LLC
  5. * @author 510974211@qq.com
  6. */
  7. namespace lemo\helper;
  8. use Ramsey\Uuid\Uuid;
  9. class StringHelper
  10. {
  11. /*
  12. 参数过滤防止攻击
  13. */
  14. public static function filterWords($str)
  15. {
  16. $farr = array(
  17. "/<(\\/?)(script|i?frame|style|html|body|title|link|meta|object|\\?|\\%)([^>]*?)>/isU",
  18. "/(<[^>]*)on[a-zA-Z]+\s*=([^>]*>)/isU",
  19. "/select\b|insert\b|update\b|delete\b|drop\b|;|\"|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile|dump/is"
  20. );
  21. $str = preg_replace($farr, '', $str);
  22. $str = strip_tags($str);
  23. return $str;
  24. }
  25. /*
  26. 参数校验
  27. */
  28. public static function filterInput($str)
  29. {
  30. if (!$str) {
  31. throw new \Exception('参数错误');
  32. }
  33. return self::filterWords($str);
  34. }
  35. /**
  36. * 生成Uuid
  37. *
  38. * @param string $type 类型 默认时间 time/md5/random/sha1/uniqid 其中uniqid不需要特别开启php函数
  39. * @param string $name 加密名
  40. * @return string
  41. * @throws \Exception
  42. */
  43. public static function uuid($type = 'time', $name = 'php.net')
  44. {
  45. switch ($type) {
  46. // 生成版本1(基于时间的)UUID对象
  47. case 'time' :
  48. $uuid = Uuid::uuid1();
  49. break;
  50. // 生成第三个版本(基于名称的和散列的MD5)UUID对象
  51. case 'md5' :
  52. $uuid = Uuid::uuid3(Uuid::NAMESPACE_DNS, $name);
  53. break;
  54. // 生成版本4(随机)UUID对象
  55. case 'random' :
  56. $uuid = Uuid::uuid4();
  57. break;
  58. // 产生一个版本5(基于名称和散列的SHA1)UUID对象
  59. case 'sha1' :
  60. $uuid = Uuid::uuid5(Uuid::NAMESPACE_DNS, $name);
  61. break;
  62. // php自带的唯一id
  63. case 'uniqid' :
  64. return md5(uniqid(md5(microtime(true) . self::randomNum(8)), true));
  65. break;
  66. }
  67. return $uuid->toString();
  68. }
  69. /**
  70. * 字符串截取
  71. */
  72. public static function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true) {
  73. // 过滤html代码
  74. $str=strip_tags($str);
  75. if(function_exists("mb_substr")){
  76. $slice = mb_substr($str, $start, $length, $charset);
  77. $strlen = mb_strlen($str,$charset);
  78. }elseif(function_exists('iconv_substr')){
  79. $slice = iconv_substr($str,$start,$length,$charset);
  80. $strlen = iconv_strlen($str,$charset);
  81. }else{
  82. $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
  83. $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
  84. $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
  85. $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
  86. preg_match_all($re[$charset], $str, $match);
  87. $slice = join("",array_slice($match[0], $start, $length));
  88. $strlen = count($match[0]);
  89. }
  90. if($suffix && $strlen>$length)$slice.='...';
  91. return $slice;
  92. }
  93. public static function htmlReplace($content){
  94. $content=str_replace("&lt;", "<", $content);
  95. $content=str_replace("&gt;", ">", $content);
  96. $content=str_replace("&namp;", "&", $content);
  97. $content=str_replace("&quot;", "\"", $content);
  98. return $content;
  99. }
  100. /**
  101. * 随机字符
  102. * @param int $length 长度
  103. * @param string $type 类型
  104. * @param int $convert 转换大小写 1大写 0小写
  105. * @return string
  106. */
  107. public static function randomNum($length=10, $type='all', $convert=0)
  108. {
  109. $config = array(
  110. 'number'=>'1234567890',
  111. 'letter'=>'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
  112. 'string'=>'abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789',
  113. 'all'=>'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
  114. );
  115. if(!isset($config[$type])) $type = 'letter';
  116. $string = $config[$type];
  117. $code = '';
  118. $strlen = strlen($string) -1;
  119. for($i = 0; $i < $length; $i++){
  120. $code .= $string{mt_rand(0, $strlen)};
  121. }
  122. if(!empty($convert)){
  123. $code = ($convert > 0)? strtoupper($code) : strtolower($code);
  124. }
  125. return $code;
  126. }
  127. /**
  128. * PHP格式化字节大小
  129. * @param number $size 字节数
  130. * @param string $delimiter 数字和单位分隔符
  131. * @return string 格式化后的带单位的大小
  132. */
  133. public static function formatBytes($size, $delimiter = '') {
  134. $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
  135. for ($i = 0; $size >= 1024 && $i < 5; $i++) $size /= 1024;
  136. return round($size, 2) . $delimiter . $units[$i];
  137. }
  138. /**
  139. * 将$name中的下划线转换成类名 全如 aa_aa 变成 AaAa
  140. * @access public
  141. * @return string
  142. */
  143. public static function formatClass($name){
  144. $temp_array = array();
  145. $arr=explode("_", $name);
  146. foreach ($arr as $key => $value) {
  147. $temp_array[]=ucfirst($value);
  148. }
  149. return implode('',$temp_array);
  150. }
  151. /**
  152. * @access public
  153. * @return string
  154. */
  155. public static function getOrderSn($str = ""){
  156. return $str.date("YmdHis",time()).sprintf('%06s', rand(0,999999));
  157. }
  158. /**
  159. * 过滤字符串中的一些内容
  160. * @access public
  161. * @return string
  162. */
  163. public static function paramFilter($value=""){
  164. $value=preg_replace("/<script[\s\S]*?<\/script>/im", "", $value);
  165. $value=preg_replace("/<script>|<\/script>/im", "", $value);
  166. return $value;
  167. }
  168. /**
  169. * 移除微信昵称中的emoji字符
  170. * @param type $nickname
  171. * @return type
  172. */
  173. public static function removeEmoji($nickname) {
  174. $clean_text = "";
  175. // Match Emoticons
  176. $regexEmoticons = '/[\x{1F600}-\x{1F64F}]/u';
  177. $clean_text = preg_replace($regexEmoticons, '', $nickname);
  178. // Match Miscellaneous Symbols and Pictographs
  179. $regexSymbols = '/[\x{1F300}-\x{1F5FF}]/u';
  180. $clean_text = preg_replace($regexSymbols, '', $clean_text);
  181. // Match Transport And Map Symbols
  182. $regexTransport = '/[\x{1F680}-\x{1F6FF}]/u';
  183. $clean_text = preg_replace($regexTransport, '', $clean_text);
  184. // Match Miscellaneous Symbols
  185. $regexMisc = '/[\x{2600}-\x{26FF}]/u';
  186. $clean_text = preg_replace($regexMisc, '', $clean_text);
  187. // Match Dingbats
  188. $regexDingbats = '/[\x{2700}-\x{27BF}]/u';
  189. $clean_text = preg_replace($regexDingbats, '', $clean_text);
  190. return trim($clean_text);
  191. }
  192. /**
  193. * @param $prifix
  194. * @param int $num
  195. * @param int $length
  196. * @return array 生成卡密
  197. */
  198. public static function getCardId($num=1,$length=10,$prifix='HX_')
  199. {
  200. //输出数组
  201. $card = array();
  202. //填补字符串
  203. $pad = '';
  204. //日期
  205. $temp = time();
  206. $Y = date('Y', $temp);
  207. $M = date('m', $temp);
  208. $D = date('d', $temp);
  209. $TD = date('YmdHis', $temp);
  210. //长度
  211. $LY = strlen((string)$Y);
  212. $LM = strlen((string)$M);
  213. $LD = strlen((string)$D);
  214. $LTD = strlen((string)$TD);
  215. //流水号长度
  216. $W = 5;
  217. //根据长度生成填补字串
  218. if ($length <= 12) {
  219. $pad = $prifix . self::randomNum($length - $W);
  220. } else if ($length > 12 && $length <= 16) {
  221. $pad = $prifix . (string)$Y . self::randomNum($length - ($LY + $W));
  222. } else if ($length > 16 && $length <= 20) {
  223. $pad = $prifix . (string)$Y . (string)$M . self::randomNum($length - ($LY + $LM + $W));
  224. } else {
  225. $pad = $prifix . (string)$TD . self::randomNum($length - ($LTD + $W));
  226. }
  227. //生成X位流水号
  228. for ($i = 0; $i < $num; $i++) {
  229. $STR = $pad . str_pad((string)($i + 1), $W, '0', STR_PAD_LEFT);
  230. $card[$i] = $STR;
  231. }
  232. return $card;
  233. }
  234. //生成密码
  235. public static function getCardPwd($num=1)
  236. {
  237. $pwd = array();
  238. for ($i = 0; $i < $num; $i++) {
  239. //生成基本随机数
  240. $charid = substr(MD5(uniqid(mt_rand(), true)), 8, 16) .self::randomNum(4, '2');
  241. $pwd[$i] = strtoupper($charid);
  242. }
  243. return $pwd;
  244. }
  245. }