国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

PHP數(shù)字和字符串ID互轉(zhuǎn)函數(shù)(類似優(yōu)酷ID)

2024-09-04 11:43:16
字體:
供稿:網(wǎng)友

這篇文章主要介紹了PHP數(shù)字和字符串ID互轉(zhuǎn)函數(shù),生成的字符串ID類似優(yōu)酷的視頻ID,像一個加密過的數(shù)字ID,本文函數(shù)可以在數(shù)字ID和字符串ID間相互轉(zhuǎn)換,需要的朋友可以參考下。

不知道你注意了沒有,類似優(yōu)酷、騰訊視頻等其他視頻鏈接似乎類似這樣的,代碼如下:

http://v.youku.com/v_show/id_XNjA5MjE5OTM2.html

注意id_xxx那段,是不是看不懂了,但你無可否認這個就是id,這不國外的一位牛人早在09年就寫了針對PHP/Python/Javascript/Java/SQL的生成方法,可見我現(xiàn)在是多么的落伍,下面我把代碼貼出來,希望分享精神永存,代碼如下:

  1. <?php 
  2. /**  
  3.  * @author   Kevin van Zonneveld <kevin@vanzonneveld.net> 
  4.  * @author   Simon Franz 
  5.  * @author   Deadfish 
  6.  * @copyright 2008 Kevin van Zonneveld (http://kevin.vanzonneveld.net) 
  7.  * @license   http://www.opensource.org/licenses/bsd-license.php New BSD Licence 
  8.  * @version   SVN: Release: $Id: alphaID.inc.php 344 2009-06-10 17:43:59Z kevin $ 
  9.  * @link   http://kevin.vanzonneveld.net/ 
  10.  * 
  11.  * @param mixed   $in      String or long input to translate 
  12.  * @param boolean $to_num  Reverses translation when true 
  13.  * @param mixed   $pad_up  Number or boolean padds the result up to a specified length 
  14.  * @param string  $passKey Supplying a password makes it harder to calculate the original ID 
  15.  * 
  16.  * @return mixed string or long 
  17.  */ 
  18. function alphaID($in$to_num = false, $pad_up = false, $passKey = null) 
  19.   $index = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  20.   if ($passKey !== null) { 
  21.       // Although this function's purpose is to just make the 
  22.       // ID short - and not so much secure, 
  23.       // with this patch by Simon Franz (http://blog.snaky.org/) 
  24.       // you can optionally supply a password to make it harder 
  25.       // to calculate the corresponding numeric ID 
  26.  
  27.       for ($n = 0; $n<strlen($index); $n++) { 
  28.           $i[] = substr$index,$n ,1); 
  29.       } 
  30.  
  31.       $passhash = hash('sha256',$passKey); 
  32.       $passhash = (strlen($passhash) < strlen($index)) 
  33.           ? hash('sha512',$passKey
  34.           : $passhash
  35.  
  36.       for ($n=0; $n < strlen($index); $n++) { 
  37.           $p[] =  substr($passhash$n ,1); 
  38.       } 
  39.  
  40.       array_multisort($p,  SORT_DESC, $i); 
  41.       $index = implode($i); 
  42.   } 
  43.  
  44.   $base  = strlen($index); 
  45.  
  46.   if ($to_num) { 
  47.       // Digital number  < 0) { 
  48.               $out -= pow($base$pad_up); 
  49.           } 
  50.       } 
  51.       $out = sprintf('%F'$out); 
  52.       $out = substr($out, 0, strpos($out'.')); 
  53.   } else { 
  54.       // Digital number  -->>  alphabet letter code 
  55.       if (is_numeric($pad_up)) { 
  56.           $pad_up--; 
  57.           if ($pad_up > 0) { 
  58.               $in += pow($base$pad_up); 
  59.           } 
  60.       } 
  61.  
  62.       $out = ""
  63.       for ($t = floor(log($in$base)); $t >= 0; $t--) { 
  64.           $bcp = bcpow($base$t); 
  65.           $a   = floor($in / $bcp) % $base
  66.           $out = $out . substr($index$a, 1); 
  67.           $in  = $in - ($a * $bcp); 
  68.       } 
  69.       $out = strrev($out); // reverse 
  70.   } 
  71.  
  72.   return $out

使用舉例,代碼如下:

alphaID(9007199254740989);

執(zhí)行結(jié)果將被返回“fE2XnNGpF”,我們可以把它認為是加密,進行反解密則,代碼如下:

alphaID('fE2XnNGpF', true);

那么就轉(zhuǎn)換成真實的數(shù)字“9007199254740989”。方法還可以支持使用key進行加密,使得別人無法解得你真實的ID。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 西乌珠穆沁旗| 湘潭县| 邹平县| 万载县| 曲沃县| 信宜市| 方正县| 上饶市| 蚌埠市| 安塞县| 武宣县| 重庆市| 华池县| 江孜县| 远安县| 汪清县| 靖西县| 沧州市| 南汇区| 嘉禾县| 屯留县| 蚌埠市| 新泰市| 保山市| 汉中市| 九龙坡区| 深水埗区| 达拉特旗| 安新县| 牙克石市| 那曲县| 江安县| 阳谷县| 囊谦县| 新邵县| 金昌市| 和田县| 威信县| 东乡族自治县| 华容县| 商城县|