| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- <?php
- //这个文件里面是处理一些常见的字符串相关的函数
- //纯工具类,不包含任何业务逻辑
- class LewaimaiString {
- //产生字母或者数字的随机数
- public static function create_noncestr($length = 16)
- {
- $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
- $str ="";
- for ( $i = 0; $i < $length; $i++ ) {
- $str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);
- }
- return $str;
- }
-
- //只产生数字的随机数
- public static function create_randnum($length = 6)
- {
- $chars = "0123456789";
- $str ="";
- for ( $i = 0; $i < $length; $i++ ) {
- $str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);
- }
- return $str;
- }
-
- /*
- * 生成以20160421这样的数字开头的唯一订单号,可能被猜到,不能用于团购券密码等地方
- */
- public static function GetUniqueTradeNo($length = 16)
- {
- if ($length < 16)
- {
- $length = 16;
- }
-
- $string = date('Ymd').substr(implode('', array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
-
- if ($length > 16)
- {
- $string .= self::create_randnum($length - 16);
- }
-
- return $string;
- }
-
- //将前缀和数字id串连起来
- //$prefix,前缀,格式如“LWMPT00000000001”
- //$id,要拼接的整数
- //$length 长度
- public static function noToStr($prefix, $id, $length = 16)
- {
- $id = $id . "";
- $idLength = strlen($id);
- return substr($prefix,0,$length-$idLength).$id;
- }
-
- //对姓名做匿名处理
- public static function niming($name)
- {
- $length = mb_strlen($name, 'utf-8');
- if($length == 0 || $length == 1)
- {
- return '***';
- }
- else
- {
- $head = mb_substr($name,0,1,'utf-8');
- $last = mb_substr($name,($length-1),1,'utf-8');
- }
-
- if ($length == 2)
- {
- return $head . "***";
- }
- else
- {
- return $head . "***" . $last;
- }
- }
-
- public static function _unserialize($string)
- {
- return unserialize(preg_replace('!s:(\d+):"(.*?)";!se', '"s:".strlen("$2").":\"$2\";"', $string));
- }
- //用于支付的唯一的订单号,长度24位
- public static function GetOutTradeNo()
- {
- $out_trade_no = "LWM" . strtoupper(uniqid()) . strtoupper(self::create_noncestr(8));
-
- return $out_trade_no;
- }
- public static function formatMonth($num , $day=0)
- {
- $year = floor($num/12);
- $month = $num%12;
- $str = $year ? $year . '年' : '';
- $str.= $month ? $month . '个月' : '';
- $day > 0 && $str .= $day.'天';
- return $str;
- }
- // 产生店铺的随机字符串
- public static function getShopNo()
- {
- $str = date('Ymd') . self::create_randnum(8);
- return $str;
- }
- }
|