技术头条 - 一个快速在微博传播文章的方式     搜索本站
您现在的位置首页 --> PHP --> TinyURL.class.php

TinyURL.class.php

浏览:3466次  出处信息

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

以下是代码片段:
<?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设计方案    (阅读:6218)
QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习
© 2009 - 2024 by blogread.cn 微博:@IT技术博客大学习

京ICP备15002552号-1