1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > php如何实现数字和字符串id互转

php如何实现数字和字符串id互转

时间:2024-03-20 10:28:00

相关推荐

php如何实现数字和字符串id互转

后端开发|PHP问题

php

后端开发-PHP问题

qq客户端 源码下载,ubuntu+方格+恢复,tomcat6的安装教程,爬虫乱码解析,php如何验证文件格式,seo 标题空格lzw

推荐:《PHP视频教学》

asp文件管理系统源码下载,vscode行宽,ubuntu网页播放视频,tomcat find,vc++ sqlite,阿里云服务器 并发数,简单优惠券插件,前端框架学几个,网络爬虫 需求分析,后缀是php,seo培训那家好,网站文章列表和图片列表排版切换代码,网页上能做出刮奖的效果吗,视频网站程序模板,在线手机页面设计,通用权限管理系统组件,赌网程序源码lzw

PHP数字和字符串ID互转函数(类似优酷ID)

财务管理系统 源码,vscode怎样安装中文包,xmind ubuntu,阿里tomcat容器,sqlite获取时间,网页设计效果图教程,ftp访问阿里云服务器,wp缓存插件,前端股票框架,爬虫怎么采集,免费php空间申请,seo实习生,springboot逻辑代码,直播源码网站,js如何获取当前网页的url,dede家具免费模板,独站宝后台,页面设计模版,集分享管理系统,java游戏程序lzw

不知道你注意了没有,类似优酷、腾讯视频等其他视频链接似乎类似这样的

代码如下:

/v_show/id_XNjA5MjE5OTM2.html

注意id_xxx那段,是不是看不懂了,但你无可否认这个就是id,这不国外的一位牛人早在09年就写了针对PHP/Python/Javascript/Java/SQL的生成方法,可见我现在是多么的落伍,下面我把代码贴出来,希望分享精神永存。

代码如下:

<?php/** * @author Kevin van Zonneveld * @author Simon Franz * @author Deadfish * @copyright Kevin van Zonneveld () * @license /licenses/bsd-license.php New BSD Licence * @version SVN: Release: $Id: alphaID.inc.php 344 -06-10 17:43:59Z kevin $ * @link / * * @param mixed $inString or long input to translate * @param boolean $to_num Reverses translation when true * @param mixed $pad_up Number or boolean padds the result up to a specified length * @param string $passKey Supplying a password makes it harder to calculate the original ID * * @return mixed string or long */function alphaID($in, $to_num = false, $pad_up = false, $passKey = null){ $index = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; if ($passKey !== null) {// Although this functions purpose is to just make the// ID short - and not so much secure,// with this patch by Simon Franz (/)// you can optionally supply a password to make it harder// to calculate the corresponding numeric ID for ($n = 0; $n<strlen($index); $n++) {$i[] = substr( $index,$n ,1);} $passhash = hash(sha256,$passKey);$passhash = (strlen($passhash) < strlen($index))? hash(sha512,$passKey): $passhash; for ($n=0; $n < strlen($index); $n++) {$p[] = substr($passhash, $n ,1);} array_multisort($p, SORT_DESC, $i);$index = implode($i); } $base = strlen($index); if ($to_num) {// Digital number > alphabet letter codeif (is_numeric($pad_up)) {$pad_up--;if ($pad_up > 0) { $in += pow($base, $pad_up);}} $out = "";for ($t = floor(log($in, $base)); $t >= 0; $t--) {$bcp = bcpow($base, $t);$a = floor($in / $bcp) % $base;$out = $out . substr($index, $a, 1);$in = $in - ($a * $bcp);}$out = strrev($out); // reverse } return $out;}

使用举例

代码如下:

<?phpalphaID(9007199254740989);

执行结果将被返回“fE2XnNGpF”,我们可以把它认为是加密,进行反解密则

代码如下:

<?phpalphaID(fE2XnNGpF, true);

那么就转换成真实的数字“9007199254740989”。方法还可以支持使用key进行加密,使得别人无法解得你真实的ID。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。