search
Homephp教程PHP源码Convert long numbers to short characters

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools