当前位置:网站首页>php生成短链接:将数字转成字母,将字母转成数字
php生成短链接:将数字转成字母,将字母转成数字
2022-04-23 06:45:00 【赫赫phper】
一、基类方法:定义字符串、编写转换方法 class NumberHelper { public static $str="abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
/** * Param:将数字转为短字符串 * User: 赫陈 * Date: 2022/4/11 * @param $number * @return string */ public static function generate_code($number) { $out = ""; $codes = self::$str; while ($number > 61) { $m = $number % 62; $out = $codes[$m].$out; $number = ($number - $m) / 62; } return $codes[$number].$out; } /** * Param:将短字符串转为数字 * User: 赫陈 * Date: 2022/4/11 * @param $string * @return float|int */ public static function get_num($string){ $codes = self::$str; $num = 0; for($i=0;$i<strlen($string);$i++){ $n = strlen($string) - $i -1; $pos = strpos($codes,$string[$i]); $num += $pos * pow(62, $n); } return $num; }
}
二、调用转换方法,根据自己定的规则,生成自己需要的短链接
public static function getShotLink($userid,$gid){ //获取用户ID短字符串 $_uid_str = NumberHelper::generate_code($userid); //获取商品ID短字符串 $_good_str = NumberHelper::generate_code($gid]); //拼接生成短链接 $param = '#A='.$_uid_str.'=B='.$_good_str.'#';
return $param
}
三、解析短链接还原参数
public static function getUnShotLink(ShotLink){ //正则匹配解析数据 $ShotLink = self::pregMatchFeng($params['share_data']); $ShotLink = explode('=',str_replace('#','',$ShotLink)); /*解字符串为数字*/ $uid = NumberHelper::get_num($ShotLink[1]); $gid = NumberHelper::get_num($ShotLink[3]);
//接着写自己的逻辑
。。。。。。
}
/** * Param: 正则匹配蜂口令 * User: 赫陈 * Date: 2022/4/12 * @param $data * @return array|mixed */ public static function pregMatchFeng($data){ $share_data = []; preg_match_all("/#A=[a-zA-Z0-9]*=B=[a-zA-Z0-9]*#/",$data, $matches); if (!empty($matches[0][0])) { $share_data = $matches[0][0]; } return $share_data; }
版权声明
本文为[赫赫phper]所创,转载请带上原文链接,感谢
https://blog.csdn.net/hechenhongbo/article/details/124217683
边栏推荐
- Redis transaction implements optimistic locking principle
- Learning records of some shooting ranges: sqli labs, upload labs, XSS
- Hierarchical output binary tree
- BUUCTF MISC刷题
- MySQL -- the secret of lock -- how to lock data
- Sto with billing cross company inventory dump return
- Research on system and software security (4)
- Feign源码分析
- 内网渗透系列:内网隧道之icmp_tran
- 高精度焊接机械臂定位
猜你喜欢
使用 Ingress 实现金丝雀发布
几种智能机器人室内定位方法对比
内网渗透系列:内网隧道之icmp_tran
nacos源码分析思路
Mobile terminal layout (3D conversion, animation)
Internal network security attack and defense: a practical guide to penetration testing (8): Authority maintenance analysis and defense
内网渗透系列:内网隧道之dns2tcp
国基北盛-openstack-容器云-环境搭建
云计算赛项--2020年赛题基础部分[任务3]
CSV Column Extract列提取
随机推荐
Buuctf misc brush questions
Dvwa 靶场练习记录
KCD_ EXCEL_ OLE_ TO_ INT_ Convert reports an error sy subrc = 2
数据库之MySQL——基本常用查询命令
Internal network security attack and defense: a practical guide to penetration testing (5): analysis and defense of horizontal movement in the domain
sentinel集成nacos动态更新数据原理
Hierarchical output binary tree
Cloud computing skills competition -- the first part of openstack private cloud environment
C read INI file and write data to INI file
C problem of marking the position of polygons surrounded by multiple rectangles
CTF-MISC总结
从ES、MongoDB、Redis、RocketMQ出发谈分布式存储
BUUCTF MISC刷題
Analysis of Nacos source code
upload-labs 靶场练习
Attack and defense world misc questions 1-50
【无标题】
Research on system and software security (3)
MySQL--锁的奥秘--数据怎么锁
[problem solving] vs2019 solves the problem that the EXE file generated by compilation cannot be opened