Home > php教程 > PHP源码 > body text

Convert long numbers to short characters

PHP中文网
Release: 2016-08-18 08:57:52
Original
2240 people have browsed it

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);
Copy after login

                    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)!

                                                            

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!