1<?PHP2 /**3 * @desc BASE62 encoding and decoding implementation4 * @param String $data5 * @author Space6 * @date 2014/8/67 **/8 9 functionBase62_encode ($data){Ten $base 62str= ' 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ '; One $data=Strval($data); A $base=Str_split($base 62str); - $len=strlen($data); - $i= 0; the $TMPARR=Array(); - while($i<$len){ - $val=$data[$i]; - $tmp=Str_pad(Decbin(Ord($data[$i]), 8, ' 0 ',str_pad_left); + $temp=Str_split($tmp, 4); - $TMPARR=Array_merge($TMPARR,$temp); +++$i; A } at $result= ' '; - $i= 0; - foreach($TMPARR as $arr){ - $temp=Bindec($arr) *4+$i%; - $result.=$base[$temp]; -++$i; in } - return $result; to } + - functionBase62_decode ($data){ the $base 62str= ' 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ '; * $data=Strval($data); $ $base=Str_split($base 62str);Panax Notoginseng $base 62ARR=Array_flip($base); - if(!Preg_match('/[a-za-z0-9]+/',$data)){ the return false; + } A $len=strlen($data); the $i= 0; + $TEMPARR=Array(); - while($i<$len){ $ $temp=Decbin(($base 62ARR[$data[$i]]-$i%2)/4); $ $TEMPARR[] =Str_pad($temp, 4, ' 0 ',str_pad_left); -++$i; - } the $result= ' '; - $TEMPARR=Array_chunk($TEMPARR, 2);Wuyi foreach($TEMPARR as $arr){ the $result.=CHR(Bindec(Join(‘‘,$arr))); - } Wu return $result; -}
It is also possible to $base62str a variable in random order according to the need, which is equivalent to a simple encryption, but it has little meaning.
PHP BASE62 encoding and decoding implementation