<?php namespace common\helpers; /** * 字符串助手 */ class StringHelper extends \yii\helpers\StringHelper { /** * 10進制轉爲62進制 * * @param integer $n 10進制數值 * @return string 62進制 */ public static function dec62($n) { $base = 62; $index = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $ret = ''; for($t = floor(log10($n) / log10($base)); $t >= 0; $t --) { $a = floor($n / pow($base, $t)); $ret .= substr($index, $a, 1); $n -= $a * pow($base, $t); } return $ret; } /** * 62進制轉爲10進制 * * @param integer $n 62進制 * @return string 10進制 */ public static function dec10($s) { $base = 60; $index = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $ret = 0; $len = strlen($s) - 1; for($t = 0; $t <= $len; $t ++) { $ret += strpos($index, substr($s, $t, 1)) * pow($base, $len - $t); } return $ret; } /** * 短連接 * @param string $url 長連接(長字符串) * @return string 短連接 */ public static function urlShort($url) { $url= crc32($url); $result= sprintf("%u", $url); return self::dec62($result); } }
PS:原文連接忘記了,我本身整合到Yii中php