3

TinyURL.class.php

 3 years ago
source link: https://blogread.cn/it/article/1708?f=hot1
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
您现在的位置首页 --> PHP --> TinyURL.class.php

TinyURL.class.php

浏览:3162次  出处信息

写了一个简单的,可以应付一些简单的项目了。

以下是代码片段:
<?php
/**
 * TinyURL生成类
 *
 * @link http://skiyo.cn
 * @author Jessica
 * @license MIT
 */
class TinyURL {
    /**
     * 组成URL的大小写字母
     *
     * @var array
     */
    protected $alpha = array();
    /**
     * 数组数量
     *
     * @var int
     */
    protected $count = 0;
    /**
     * 构造器 生成URL数组
     *
     * @access public
     */
    public function  __construct() {
        $this->alpha = array_merge(range(0, 9), range(’a’, ’z’), range(’A’, ’Z’));
        $this->count = count($this->alpha);
    }
    /**
     * 通过数字生成唯一的URL
     *
     * @param int $num
     * @return string
     */
    public function getURL($num) {
        $num = (int)$num;
        $url = ’’;
        while($num >= 1) {
            $url .= $num < $this->count ? $this->alpha[$num] : $this->alpha[$num%$this->count];
            $num = (int)($num)/$this->count;
        }
        $url =  strrev($url);
        return $url;
    }
}
/**
 * @example
 */
$t = new TinyURL();
for($i=1;$i<=10000;$i++) {
    echo $t->getURL($i). ’<br />’;
}

建议继续学习:

  1. TinyURL设计方案    (阅读:5923)
QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK