PHP把数字ID转字母ID .
分类: php 2012-04-21 17:00 1661人阅读 评论(0) 收藏 举报
phpuptranslationstringfunctionnumbers ID是网站中经常出现的,它一般是数字,但是我们发现现在的网站很多ID都是字母了,比如YouTube的视频播放页它的URL类似/watch?v=yzNjIBEdyww。 下面是一个生成字母ID的方法。
使用示例:
[php] view plaincopyprint?
01.alphaID(12354); //会将数字转换为字母。
02.alphaID('PpQXn7COf',true);//会将字母ID转换为对应的数字。
03.alphaID(12354,false,6);//指定生成字母ID的长度为6.
alphaID(12354); //会将数字转换为字母。
alphaID('PpQXn7COf',true);//会将字母ID转换为对应的数字。
alphaID(12354,false,6);//指定生成字母ID的长度为6.源码:
[php] view plaincopyprint?
01.<?php
02./**
03. * Translates a number to a short alhanumeric version
04. *
05. * Translated any number up to 9007199254740992
06. * to a shorter version in letters e.g.:
07. * 9007199254740989 --> PpQXn7COf
08. * 文库分享网(www.Wkfxw.com),全免费下载
09. * specifiying the second argument true, it will
10. * translate back e.g.:
11. * PpQXn7COf --> 9007199254740989
12. *
13. * this function is based on any2dec && dec2any by
14. * fragmer[at]mail[dot]ru
15. * see: http://nl3.php.net/manual/en/function.base-convert.php#52450
16. *
17. * If you want the alphaID to be at least 3 letter long, use the
18. * $pad_up = 3 argument
19. *
20. * In most cases this is better than totally random ID generators
21. * because this can easily avoid duplicate ID's.
22. * For example if you correlate the alpha ID to an auto incrementing ID
23. * in your database, you're done.
24. *
25. * The reverse is done because it makes it slightly more cryptic,
26. * but it also makes it easier to spread lots of IDs in different
27. * directories on your filesystem. Example:
28. * $part1 = substr($alpha_id,0,1);
29. * $part2 = substr($alpha_id,1,1);
30. * $part3 = substr($alpha_id,2,strlen($alpha_id));
31. * $destindir = "/".$part1."/".$part2."/".$part3;
32. * // by reversing, directories are more evenly spread out. The
33. * // first 26 directories already occupy 26 main levels
34. *
35. * more info on limitation:
36. * - http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/165372