当前位置:网站首页>PHP generates short links: convert numbers to letters and letters to numbers
PHP generates short links: convert numbers to letters and letters to numbers
2022-04-23 08:07:00 【Hehe PHPer】
One 、 Base class method : Define string 、 Write conversion methods class NumberHelper { public static $str="abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
/** * Param: Convert a number to a short string * User: He Chen * 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: Convert a short string to a number * User: He Chen * 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; }
}
Two 、 Call conversion method , According to your own rules , Generate short links you need
public static function getShotLink($userid,$gid){ // Get users ID Short string $_uid_str = NumberHelper::generate_code($userid); // Access to goods ID Short string $_good_str = NumberHelper::generate_code($gid]); // Splice to generate short links $param = '#A='.$_uid_str.'=B='.$_good_str.'#';
return $param
}
3、 ... and 、 Resolve short link restore parameters
public static function getUnShotLink(ShotLink){ // Regular matching parsing data $ShotLink = self::pregMatchFeng($params['share_data']); $ShotLink = explode('=',str_replace('#','',$ShotLink)); /* Solution to a number string */ $uid = NumberHelper::get_num($ShotLink[1]); $gid = NumberHelper::get_num($ShotLink[3]);
// Then write your own logic
......
}
/** * Param: Regular matching bee password * User: He Chen * 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; }
版权声明
本文为[Hehe PHPer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230645420096.html
边栏推荐
猜你喜欢
使用 Ingress 实现金丝雀发布
利用sqlmap注入获取网址管理员账号密码
CTF攻防世界刷题51-
数据库之Mysql——概述安装篇
在线YAML转XML工具
DVWA靶场练习
简述存储器的分级策略
[programming practice / embedded competition] learning record of embedded competition (I): establishment of TCP server and web interface
SAP sto with billing process and configuration
【编程实践/嵌入式比赛】嵌入式比赛学习记录(二):基于TCP的图片流传输
随机推荐
输入 “ net start mysql ”,出现 “ 发生系统错误 5。 拒绝访问 ” 。问题详解
Jetson Xavier NX(3)Bazel Mediapipe 安装
Intranet security attack and defense: a practical guide to penetration testing (6): domain controller security
Reptile learning notes, learning reptile, read this article is enough
SAP tr manual import system operation manual
Intranet penetration series: icmptunnel of Intranet tunnel (by master dhavalkapil)
Internal network security attack and defense: a practical guide to penetration testing (5): analysis and defense of horizontal movement in the domain
Concours de compétences en informatique en nuage - - première partie de l'environnement cloud privé openstack
Summary of facial classics
Intranet penetration series: dns2tcp of Intranet tunnel
Construction of middleman environment mitmproxy
Feign source code analysis
Intranet penetration series: ICMP of Intranet tunnel_ Tran
Go语学习笔记 - Slice、Map | 从零开始Go语言
Intranet penetration series: pingtunnel of Intranet tunnel
strcat()、strcpy()、strcmp()、strlen()
Cloud computing skills competition -- Part 2 of openstack private cloud environment
利用sqlmap注入获取网址管理员账号密码
学fpga(从verilog到hls)
Mobile terminal layout (3D conversion, animation)