zoukankan      html  css  js  c++  java
  • 短网址

    思路一:

    <?php
    function shorturl($url='') {
    $base = array (
                  "a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" ,
                  "i" , "j" , "k" , "l" , "m" , "n" , "o" , "p" , 
                  "q" , "r" , "s" , "t" , "u" , "v" , "w" , "x" , 
                  "y" , "z" , "0" , "1" , "2" , "3" , "4" , "5" ,
                  "6" , "7" , "8" , "9" , "A" , "B" , "C" , "D" , 
                  "E" , "F" , "G" , "H" , "I" , "J" , "K" , "L" , 
                  "M" , "N" , "O" , "P" , "Q" , "R" , "S" , "T" ,
                  "U" , "V" , "W" , "X" , "Y" , "Z");
    
    $hex = md5($url);
    $hexLen = strlen($hex);
    $subHexLen = $hexLen / 8;
    $output = array();
    
    for ($i = 0; $i < $subHexLen; $i++) {
        $subHex = substr ($hex, $i * 8, 8);
        $int = 0x3FFFFFFF & (1 * ('0x'.$subHex));
        $out = '';
        for ($j = 0; $j < 6; $j++) {
            $val = 0x0000003D & $int;//因为6个字符的每个字符有26+26+10个组合
            $out .= $base[$val];
            $int = $int >> 5;//取$int30位长度,每次右移5位,正好可以得到6个组
        }
        $output[] = $out;
        }
        return $output;
    }
    
    $urls = shorturl('http://www.php100.com');
    echo "<pre>";var_dump($urls);exit();
    ?>

    思路二:

    另一种是存数据库中,取自增ID,进行10=》62进制转换,结果为短网址,访问时再从数据库取得真实地址。

  • 相关阅读:
    name mangling
    Haskell: What is Weak Head Normal Form
    取模运算和取余运算的区别
    a common method to rotate the image
    代码静态分析工具
    LeeCode-Single Number III
    七夕这天
    mysql TO_DAYS()
    (转)剖析Linux文件编码的查看及修改
    docker
  • 原文地址:https://www.cnblogs.com/thinksasa/p/4248941.html
Copyright © 2011-2022 走看看