Home >php教程 >PHP源码 >Convert long numbers to short characters

Convert long numbers to short characters

PHP中文网
PHP中文网Original
2016-08-18 08:57:522304browse

Convert long numbers to short characters


/**
 * 将数字转为短网址代码
 *
 * @param int $number 数字
 * @return string 短网址代码
 */
function generate_code($number) {
  $out  = "";
  $codes = "abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  while ($number > 60) {
    $key  = bcmod($number,'61');
    $number = bcsub(bcp($number,'61'),'1');
    $out  = $codes{$key}.$out;
  }
  return $codes{$number}.$out;
}
/**
 * 将短网址代码转为数字
 *
 * @param string $code 短网址代码
 * @return int 数字
 */
function get_num($code){
  $codes = "abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  $num = 0;
  $i = strlen($code);
 for($j=0;$j<strlen($code);$j++){
    $i--;
    $char = $code{$j};
    $pos = strpos($codes,$char);
    $num = bcadd(bcmul(bcpow("61", $i),($pos + 1)),$num);
  }
  $num=bcsub($num,"1");
  return  $num;
}
/*****函数结束****/

$id="1973337397412392446";
echo $id."
";
$did=generate_code($id);
echo generate_code($id)."
";
echo get_num($did);

                    The above is the content of converting long numbers to short characters. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

                                                            

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn