alex321
2017-03-09 08:47:44 +08:00
function getShortUrl($url, $reback = false){
if($reback){
// 查询数据库
$this->db->where('surl', $url);
$data = $this->db->get('surl');
if($data->num_rows() > 0){
$data = $data->row_array(1);
return $data['lurl'];
}
} else{
$result = sprintf("%u", crc32($url));
$sUrl= '';
while($result>0){
$s = $result%62;
if($s>35){
$s = chr($s+61);
} elseif($s>9 && $s<=35){
$s = chr($s+ 55);
}
$sUrl .= $s;
$result = floor($result/62);
}
// 插入数据库
$query = $this->db->insert_string('surl', array(
'surl' => $sUrl,
'lurl' => $url
));
$this->db->query(str_replace('INSERT INTO','INSERT IGNORE INTO', $query));
return $sUrl;
}
}