首页 后端开发 php教程 PHP把数目字ID转字母ID

PHP把数目字ID转字母ID

Jun 13, 2016 am 11:34 AM
index out quot substr

PHP把数字ID转字母ID

PHP把数字ID转字母ID

 ID是网站中经常出现的,它一般是数字,但是我们发现现在的网站很多ID都是字母了,比如YouTube的视频播放页它的URL类似/watch?v=yzNjIBEdyww。 下面是一个生成字母ID的方法。
使用示例:

echo alphaID(12354);  //qnd  echo alphaID('qnd',true); //12354  echo alphaID(12354,false,6); //qndaab  echo alphaID('qndaab',true, 6); //12354 
登录后复制

源码:

<?php /** * Translates a number to a short alhanumeric version * * Translated any number up to 9007199254740992 * to a shorter version in letters e.g.: * 9007199254740989 --> PpQXn7COf * * specifiying the second argument true, it will * translate back e.g.: * PpQXn7COf --> 9007199254740989 * * this function is based on any2dec && dec2any by * fragmer[at]mail[dot]ru * see: http://nl3.php.net/manual/en/function.base-convert.php#52450 * * If you want the alphaID to be at least 3 letter long, use the * $pad_up = 3 argument * * In most cases this is better than totally random ID generators * because this can easily avoid duplicate ID's. * For example if you correlate the alpha ID to an auto incrementing ID * in your database, you're done. * * The reverse is done because it makes it slightly more cryptic, * but it also makes it easier to spread lots of IDs in different * directories on your filesystem. Example: * $part1 = substr($alpha_id,0,1); * $part2 = substr($alpha_id,1,1); * $part3 = substr($alpha_id,2,strlen($alpha_id)); * $destindir = "/".$part1."/".$part2."/".$part3; * // by reversing, directories are more evenly spread out. The * // first 26 directories already occupy 26 main levels * * more info on limitation: * - http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/165372 * * if you really need this for bigger numbers you probably have to look * at things like: http://theserverpages.com/php/manual/en/ref.bc.php * or: http://theserverpages.com/php/manual/en/ref.gmp.php * but I haven't really dugg into this. If you have more info on those * matters feel free to leave a comment. * * @author  Kevin van Zonneveld  * @author  Simon Franz * @author  Deadfish * @copyright 2008 Kevin van Zonneveld (http://kevin.vanzonneveld.net) * @license   http://www.opensource.org/licenses/bsd-license.php New BSD Licence * @version   SVN: Release: $Id: alphaID.inc.php 344 2009-06-10 17:43:59Z kevin $ * @link    http://kevin.vanzonneveld.net/ * * @param mixed   $in    String 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 function's purpose is to just make the    // ID short - and not so much secure,    // with this patch by Simon Franz (http://blog.snaky.org/)    // you can optionally supply a password to make it harder    // to calculate the corresponding numeric ID     for ($n = 0; $n<strlen substr hash strlen : for array_multisort sort_desc implode if digital number alphabet letter code strrev bcpow strpos> 0) {        $out -= pow($base, $pad_up);      }    }    $out = sprintf('%F', $out);    $out = substr($out, 0, strpos($out, '.'));  } else {    // Digital number  -->>  alphabet letter code    if (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;}</strlen>
登录后复制


本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

index.html是什么文件? index.html是什么文件? Feb 19, 2024 pm 01:36 PM

index.html代表网页的首页文件,是网站的默认页面。当用户访问一个网站时,通常会首先加载index.html页面。HTML(HypertextMarkupLanguage)是一种用于创建网页的标记语言,index.html也是一种HTML文件。它包含网页的结构和内容,以及用于格式化和布局的标签和元素。下面是一个示例的index.html代码:&lt

主板上的数字音频输出接口-SPDIF OUT 主板上的数字音频输出接口-SPDIF OUT Jan 14, 2024 pm 04:42 PM

主板上SPDIFOUT连接线序最近我遇到了一个问题,就是关于电线的接线顺序。我上网查了一下,有些资料说1、2、4对应的是out、+5V、接地;而另一些资料则说1、2、4对应的是out、接地、+5V。最好的办法是查看你的主板说明书,如果找不到说明书,你可以使用万用表进行测量。首先找到接地,然后就可以确定其他的接线顺序了。主板vdg怎么接线连接主板的VDG接线时,您需要将VGA连接线的一端插入显示器的VGA接口,另一端插入电脑的显卡VGA接口。请注意,不要将其插入主板的VGA接口。完成连接后,您可以

out和in接口是什么意思 out和in接口是什么意思 Sep 28, 2021 pm 04:39 PM

out接口指的是输出接口,in接口指的是输入接口。out接口一般代表着音源线路输出接口,用来接负载,例音箱、耳机等;而in接口一般代表着音源线路输入接口,用来接CD机、手机、MP3、电脑等。

PHP返回字符串第一个字符的 ASCII 值 PHP返回字符串第一个字符的 ASCII 值 Mar 21, 2024 am 11:01 AM

这篇文章将为大家详细讲解有关PHP返回字符串第一个字符的ASCII值,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。PHP返回字符串第一个字符的ASCII值引言在php中,获取字符串第一个字符的ASCII值是一个常见的操作,涉及到字符串处理和字符编码基础知识。ASCII值用于表示字符在计算机系统中的数字值,对于字符比较、数据传输和存储至关重要。过程获取字符串第一个字符的ASCII值涉及以下步骤:获取字符串:确定要获取ASCII值的字符串。它可以是变量、字符串常量

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

PHP返回一个字符串在另一个字符串中开始位置到结束位置的字符串 PHP返回一个字符串在另一个字符串中开始位置到结束位置的字符串 Mar 21, 2024 am 10:31 AM

这篇文章将为大家详细讲解有关PHP返回一个字符串在另一个字符串中开始位置到结束位置的字符串,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。PHP中使用substr()函数从字符串中提取子字符串substr()函数可从字符串中提取指定范围内的字符。其语法如下:substr(string,start,length)其中:string:要从中提取子字符串的原始字符串。start:子字符串开始位置的索引(从0开始)。length(可选):子字符串的长度。如果未指定,则提

PHP源码运行问题:index报错解决方法 PHP源码运行问题:index报错解决方法 Mar 09, 2024 pm 09:24 PM

PHP源码运行问题:index报错解决方法,需要具体代码示例PHP是一种广泛使用的服务器端脚本语言,经常被用于开发动态网站和Web应用程序。然而,有时候在运行PHP源码时会遇到各种问题,其中“index报错”是比较常见的一种情况。本文将介绍一些常见的index报错原因以及解决方法,并提供具体的代码示例,帮助读者更好地处理这类问题。问题描述:在运行PHP程序时

win10系统闪退显示out of memory怎么办? win10系统闪退显示out of memory怎么办? Feb 09, 2024 pm 03:00 PM

win10系统闪退显示outofmemory,近期很多的用户在使用电脑的时候,出现了这个提示,导致需要经常的重启进行修复,那么这种情况我们应该如何处理,针对这个问题,本期的win10教程就来和广大用户们分享完整操作步骤,希望能够帮助更多的小伙伴解决问题。win10系统闪退显示outofmemory怎么办1、右击桌面上的此电脑,选择选项列表中的“属性”。2、进入到新的窗口界面后,点击左上角的“高级系统设置”选项。3、在打开的窗口中,切换到上方中的“

See all articles